R
R
River00512018-05-22 23:47:03
MySQL
River0051, 2018-05-22 23:47:03

What is the best way to parse from file to wordpress database?

Hello, I need to periodically parse a file with thousands of lines into the database, the columns are tab-separated, into a new table.
I found this code

function create_table() {
  global $wpdb;

  require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

  $table_name = $wpdb->get_blog_prefix() . 'test_table';
  $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset} COLLATE {$wpdb->collate}";

  $sql = "CREATE TABLE {$table_name} (
  id  bigint(20) unsigned NOT NULL auto_increment,
  address varchar(255) NOT NULL default '',
  alert varchar(20) NOT NULL default '',
  meta longtext NOT NULL default '',
  PRIMARY KEY  (id),
  KEY alert (alert)
  )
  {$charset_collate};";

  dbDelta($sql);
}

create_table();

LOAD DATA LOCAL INFILE 'mytabdelimitedfile.txt'
INTO table_name
FIELDS
  TERMINATED BY '\t'
  OPTIONALLY ENCLOSED BY '"'
(col1,col2,col3,col4,col5)

I would like to implement this as a plugin, or maybe you can tell me something better
Initially I wanted to do this, go to the plugin settings
From above, it immediately suggests uploading a file
The file is uploaded to the database, on the same page something like "Running, data uploaded / updated" is written "
Help me get the code right

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2018-05-23
@HeadOnFire

For high volumes, when timeouts threaten, use WP-CLI

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question