dongyoulou4829 2013-04-10 19:14
浏览 438
已采纳

在LOAD DATA INFILE期间,时间戳字段未正确更新

I'm populating a MySQL table with test data using LOAD DATA INFILE and seeing behavior I don't understand. The timestamp column (with default=CURRENT_TIMESTAMP) is updating some records correctly and others, seemingly randomly, with 0000-00-00 00:00:00.

Questions Table Structure:
Column             Type     Null    Default             Comments
Q_ID               int(10)  No           
Clients_ID         int(10)  No           
Question           varchar(255) No  

. . other fields left out .

Submit_Timestamp   timestamp    No      CURRENT_TIMESTAMP 

After connecting to the db, here are the relevant lines of code:

// prepare the SQL query for the Questions table

$sql = "LOAD DATA INFILE 'c:/wamp/www/Q_Sample_01.CSV' INTO TABLE Questions FIELDS TERMINATED BY ','";

// submit the query and capture the result

$result = mysqli_query($conn, $sql) or die (mysqli_error($conn));

The program executes and the data is INSERTed into all fields properly with the exception of the Submit_Timestamp field, which sometimes gets correct results, and sometimes gets 0000-00-00 00:00:00.

The CSV file has a leading comma in all records to indicate the missing Q_ID data. The Q_ID field, which is Autoincrement, is working properly. There is no data for timestamp at the end of the CSV rows, I expect MySQL to enter the default value. I tried to put trailing comas after the last field listed in the CSV, thinking that might make it more clear there was data to add into the Submit_Timestamp field, but it made things worse, giving me 0000-00-00 00:00:00 for every record.

I also tried a suggestion to alter the table making Submit_Timestamp ON UPDATE CURRENT_TIMESTAMP, but it didn't help, and isn't the behavior I want for this field, so I removed it.

I just noticed a pattern, though it's not yet explanatory to me yet. The records that get all 0s for the timestamp have at least one field in them that has double quotes around it, to prevent the included comma from inappropriately indicating a new field. This is not always in the last field (adjacent to the Submit_Timestamp field). The CSV was created in Microsoft Excel.

Where am I going wrong?

  • 写回答

1条回答 默认 最新

  • du3979 2013-04-10 23:38
    关注

    Excel save to CSV is problematic with string data that contains commas.

    You can easily fix this by changing the separator character to something not in the data. I used the '|' (above the backslash on my keyboard).

    You'll also need to change your SQL command to incorporate this change to the separator character.

    $sql = "LOAD DATA INFILE 'c:/wamp/www/Q_Sample_01.CSV' INTO TABLE Questions FIELDS TERMINATED BY '|'";

    This worked perfectly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集