douzi115522 2014-01-12 08:09
浏览 131

我的LOAD DATA INFILE语法有什么问题?

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 ' ' ( 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 ));

  • 写回答

2条回答 默认 最新

  • dongyin2390 2014-01-12 08:17
    关注

    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 '
    ' ( `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 
              '
    ' ( `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 
              '
    ' ( `userID`, `movieID`, `rate`, `timestamp`)
    ");
    

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

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line