简体   繁体   English

MySQL通过LOAD DATA INFILE从CSV文件导入数据不起作用

[英]MySQL import data from a csv file via LOAD DATA INFILE does not work

I tried so hard to get this command work. 我非常努力地使此命令起作用。 Nothing is happening. 没事 Here is my issue: 这是我的问题:

I have a .csv file which I am uploading to my server where I want to run a script to import all of the data into a MySQL table. 我有一个.csv文件,该文件要上传到服务器上,我要在该服务器上运行脚本以将所有数据导入到MySQL表中。 The csv looks like this: csv看起来像这样:

id;herstellernr;hersteller;firmenname;url
123;ABC;Hersteller1;Firmenname1;http://www.test.com
234;DEF;Hersteller2;Firmenname2;http://www.test2.de
345;GHI;Hersteller3;Firmenname3;http://www.test3.net

... and so on. ... 等等。

After uploading the csv file, I first want to create a new table (temporary) where my data should be imported. 上载csv文件后,我首先要创建一个新表(临时表),在该表中应导入我的数据。 Creating the table works without any problems; 创建表的过程没有任何问题; getting my Data imported from the csv file doesn't. 没有从csv文件导入我的数据。

Here is my script (which comes after the upload script): 这是我的脚本(在上传脚本之后):

$temp = $pdo->prepare("

CREATE TABLE ".$name."
(
   id int(11),
   herstellernr varchar(3),
   hersteller varchar(150),
   firmenname varchar(255),
   url varchar(100),
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

LOAD DATA INFILE '_csv/".$filename."' INTO TABLE ".$name."
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id, herstellernr, hersteller, firmenname, url)

");

$temp->execute(array());

CREATE TABLE works but after that there is nothing happening with my LOAD DATA INFILE CREATE TABLE有效,但是之后我的LOAD DATA INFILE没有任何反应

Plus: I don't really know if it should be '\\n' or '\\r\\n'. 加:我真的不知道它应该是'\\ n'还是'\\ r \\ n'。 Anyway both cases didn't work. 无论如何,这两种情况都不起作用。

I tried really hard and I am sure the syntax is just right. 我真的很努力,我确定语法是正确的。 But I can't make it. 但是我做不到。 Can you help? 你能帮我吗? Thank you 谢谢

You cannot use multiple queries with PDO. 您不能对PDO使用多个查询。 You'll need to break them up into separate queries. 您需要将它们分解为单独的查询。 Since you're not using bound parameters, you can skip the prepare, as well: 由于您没有使用绑定参数,因此也可以跳过prepare:

$pdo->query("CREATE TABLE ".$name."
(
   id int(11),
   herstellernr varchar(3),
   hersteller varchar(150),
   firmenname varchar(255),
   url varchar(100),
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");

$pdo->query("LOAD DATA INFILE '_csv/".$filename."' INTO TABLE ".$name."
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id, herstellernr, hersteller, firmenname, url)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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