简体   繁体   中英

Load Data Infile OPTIONALLY ENCLOSED BY not working as expected

Many related tickets and tut sites but unfortunately no solutions have worked for me so far, couple hours stuck on this.

I have many csv files I need to insert into a mysql db, using 'load in file' does it very quickly and easily.

However, it stumbles on one section: Sample csv line:

ABSOLUTE,Glenn,Anakie,"Cool Synd, Super Cool"

Code I'm using is:

$q = "load data infile '$file' into table $table
    FIELDS TERMINATED BY ','
    OPTIONALLY ENCLOSED BY '\"'
    LINES TERMINATED BY '\n';";
    mysql_query($q, $db);

I expect to have 4 fields from this, but I end up with 5, it counts the comma inside the quotes as being a delimiter. I thought the point of OPTIONALLY ENCLOSED BY was to catch these cases?

I've tried switching the order of the field terminated and opt enclosed by commands. Tried just making it ENCLOSED BY. Tried not escaping the ", also /"/", tried using 2 single quotes instead of a double; but I still can't get it to understand that I want "Cool Synd, Super Cool" as one field.

Anyone know what I'm doing wrong here?

Here is my table structure:

在此输入图像描述

I simply throw all my data in here then I process it and move fields to the relevant tables into the correct format. The column with the problem is 'Owner'. I've changed it to text type here as that was the solution for others, but it works the same way whether it is text, char, varchar, etc.

(Working on Ubuntu server 14.4, Apache/2.4.7, PHP 5.5.9)

I had this exact problem and solved it here:

https://arstechnica.com/civis/viewtopic.php?t=366658

Just reverse the order.

$q = "load data infile '$file' into table $table
    FIELDS OPTIONALLY ENCLOSED BY '"'
    TERMINATED BY ','
    LINES TERMINATED BY '\n';";
    mysql_query($q, $db);

You don't need the backslash before the doublequote either

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