简体   繁体   中英

Load data.csv when installing plugin

I am new to wordpress plugin development. I want to create a new table when someone installs the plugin and populate the daata from csv file into the table.

Here is my code to create table:

$sql = "CREATE TABLE $table_name (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `location` varchar(150) NOT NULL,
    `slug` varchar(150) NOT NULL,
    `population` int(10) unsigned NOT NULL,
       PRIMARY KEY (id) 
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8";

$wpdb->query($sql);

After running this table is created.

Issue is that I am finding it difficult to populate data.csv into the table.

In windows xampp it is working

$current_plugin_dir = dirname(__FILE__);
$current_plugin_dir = str_replace("\\","\\\\",$current_plugin_dir);
$current_plugin_dir = $current_plugin_dir . '\\\\data.csv';
$sql = "LOAD DATA LOCAL INFILE '$current_plugin_dir' INTO TABLE $table_name";
$wpdb->query($sql);

While live server it is not working... But following code works...

$current_plugin_dir = $current_plugin_dir.'/data.csv';
$sql = "LOAD DATA LOCAL INFILE '$current_plugin_dir' INTO TABLE $table_name";
$wpdb->query($sql);

But in my ubuntu operating system no one seems to work.. I debugged and it seems following query ran on ubuntu lamp environment

LOAD DATA LOCAL INFILE '/var/www/html/testwp/wp-content/plugins/plugin-name/data.csv' INTO TABLE test_population

and it triggered following error

1148 - The used command is not allowed with this MySQL version

Is there any single way of loading csv on all platforms? Any help?

You can specify it as an additional option when setting up your connection:

mysql -u myuser -p --local-infile yourdatabase

or put it in your my.cnf file:

[client]
loose-local-infile=1

This is because that feature opens a security hole and is disabled by default. So you have to enable it manually in case you want to use it.

An other option is to read file by using fopen() and usual sql inserts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM