简体   繁体   中英

What is wrong in my LOAD DATA INFILE Syntax?

I'm trying to import a CSV file into MySQL database but it didn't work.

I have 5 columns in my database:

id , userID, movieID, rate, timestamp

And in my CSV file I have only 4 columns separated by two colons '::'.
This is the sample data:

1::1193::5::978300760

This is my query

$query = "LOAD DATA INFILE '$uploadfile' INTO TABLE rating_table FIELDS TERMINATED BY '::' LINES TERMINATED BY '\\n' ( userID, movieID, rate, timestamp)";

And I'm using PDO_MySQL this is the additional code so that you can understand.

$stmt = $dbh->prepare($query);

try{
        if($stmt->execute()){
                echo "Sucessful importing of CSV file into the MySQL database!";
            }

    }
catch (PDOException $e) {
    echo "Error importing of CSV file into the MySQL database!";
    echo "Error!: " . $e->getMessage() . "<br/>";
    die();
}
echo "</p>";  

I've tried many times but when I've checked in my database, it is still empty.
I can't seem to figure out what is wrong, can you help me? Thanks!

This is the error I've got.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.' in C:\\xampp\\htdocs\\movie\\php\\upload_file.php:57 Stack trace: #0 C:\\xampp\\htdocs\\movie\\php\\upload_file.php(57): PDOStatement->execute() 1 {main} thrown in C:\\xampp\\htdocs\\movie\\php\\upload_file.php on line 57

This is my connection settings:

$dbh = new PDO('mysql:host=localhost;dbname=movie','root', '', array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8", PDO::ATTR_EMULATE_PREPARES => FALSE, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_LOCAL_INFILE => TRUE ));

In order to see error messages on your output, just initialize your PDO with PDO::ERRMODE_EXCEPTION , something like this:

$db = new PDO('mysql:host=' . $config['db_host'] . ';dbname=' . $config['db_name'], $config['db_username'], $config['db_password'],
    array(
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8",
        PDO::ATTR_EMULATE_PREPARES => FALSE,
        PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        ));

Afterwards you will be able to see the error. Post it in an update and I/we will be able to help you out!

EDIT 1:

Take a look at the manual on PDO_MYSQL .

You might also need:

PDO::MYSQL_ATTR_LOCAL_INFILE => TRUE

Note, this constant can only be used in the driver_options array when constructing a new database handle.

EDIT 2:

According to MySQL Manual , MySQL must be compiled with --enable-local-infile :

You MUST have compiled PHP using the full path to MySQL, otherwise it will use it's internal handlers, which don't work with the "new" LOAD DATA.

--with-mysql=/usr/local/mysql (assuming your MySQL is located here)

You MUST start the MySQL daemon with the option '--local-infile=1'

Or enabling it, by adding to your /etc/my.cnf :

[mysql]
local-infile=1

[mysqld]
local-infile=1

Restart mysqld afterwards, by running service mysqld restart .

EDIT 3:

Quotes are for string literals, back-ticks for columns and tables names. Escape a variable properly. Change your query to:

$query = "LOAD DATA INFILE ' . $uploadfile . ' INTO TABLE `rating_table` FIELDS TERMINATED BY '::' LINES TERMINATED BY '\n' ( `userID`, `movieID`, `rate`, `timestamp`)";

EDIT 4:

This error is normally caused when you have multiple queries open at the same time. For example, you call fetch() , but you don't call it until it's finished, and then you try to execute a second query.

Normally, the solution to this problem is to call closeCursor() PHP: PDOStatement::closeCursor - Manual . Try calling that:

/* The following call to closeCursor() may be required by some drivers */
$stmt->closeCursor();

/* Now we can execute the second statement */
$otherStmt->execute();

and see if that changes anything for you.

EDIT 5:

Try using other syntax for escaping variable. Use the following:

$stmt = $dbh->prepare("
       LOAD DATA INFILE 
          '" . $uploadfile . "' 
       INTO TABLE 
          `rating_table`
       FIELDS TERMINATED BY 
          '::' 
       LINES TERMINATED BY 
          '\n' ( `userID`, `movieID`, `rate`, `timestamp`)
");
$stmt->execute();

EDIT 6:

PDO placeholders can only be used for values, and not database, table or column names! I think the error is coming from using '::' . Change your data delimiter in your files from '::' to, let's say '.' .

As quick workaround for checking you could use your query directly without prepare:

$stmt = $dbh->query("
       LOAD DATA INFILE 
          '" . $uploadfile . "' 
       INTO TABLE 
          `rating_table`
       FIELDS TERMINATED BY 
          '::' 
       LINES TERMINATED BY 
          '\n' ( `userID`, `movieID`, `rate`, `timestamp`)
");

If it doesn't work try changing data delimiter or try escaping your existing delemiter ( :: ) somehow.

You are probably missing LOCAL keyword in your LOAD DATA INFILE statement.

LOCAL means that the file you are loading from is located on the client and not on the server. When I just tried this, with your table and file, it worked fine.

> cat infile.csv 
1::1193::5::978300760

> mysql
mysql > create table ratings (
    id int primary key auto_increment, 
    userID int, 
    movieID int, 
    rate int, 
    ts long);
Query OK, 0 rows affected (0.06 sec)

mysql > LOAD DATA LOCAL INFILE 'infile.csv' INTO TABLE ratings FIELDS TERMINATED BY '::' LINES TERMINATED BY '\n' ( userID, movieID, rate, ts);
Query OK, 1 row affected (0.00 sec)
Records: 1  Deleted: 0  Skipped: 0  Warnings: 0

mysql > select * from ratings;
+----+--------+---------+------+-----------+
| id | userID | movieID | rate | ts        |
+----+--------+---------+------+-----------+
|  1 |      1 |    1193 |    5 | 978300760 |
+----+--------+---------+------+-----------+
1 row in set (0.00 sec)

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