Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question