MySQL CSV Import - date entered as 0000-00-00 00:00:00 if timestamp has milliseconds?

北慕城南 提交于 2019-12-23 17:39:56

问题


I currently have a large number of CSVs to import to a MySQL database. The files contain timestamps for each record, which are in the format (for example):

2011-10-13 09:36:02.297000000

I am aware of the MySQL bug #8523, which indicates that storing milliseconds in a datetime field is not supported. Despite this, I would have expected the datetime field to truncate the record after the seconds, instead of being entered as blank.

I have narrowed down the problem to the milliseconds (as opposed to the formatting of the csv etc.), since

2011-10-13 09:36:02

imports correctly.

Could anyone suggest a way that I can get this data imported without zeros? I have too many CSVs to go into each manually and adjust the length/formatting of the timestamps.

I should point out that while milliseconds would be a nice-to-have, they are not necessary to my application, so I would be happy with a solution that allows me to easily truncate the numbers and import them.

Thanks!

EDIT: To clarify, I am importing the CSVs using the following command:

mysqlimport --fields-enclosed-by="" --fields-terminated-by="," --lines-terminated-by="\n" --columns=id,@x,Pair,Time -p --local gain [file].csv

This is very fast for importing the records - I have around 50m to import, so reading each line in is not a great option.


回答1:


I don't know how are you importing the CSVs but the way I would do is to write a script (php/perl) to read each file, round up or trim the time stamp to seconds and execute INSERT statements on the DATABASE.

Something like

<?php
$file=fopen("your.csv","r");
mysql_connect ($ip, $user, $pass);

while(!feof($file))
{
   $line = explode(',',fgets($file));
   mysql_query("INSERT INTO TABLE1 (ID, DATE) values (".$line[0].", ".substr($line[1],0,19).")");
}
fclose($file);
?>

Execute this from the command line and it should do the job




回答2:


It will not import when using the milliseconds, but it does import without. So you have to substring one way or another. MySQL has various string functions, such as SUBSTRING - which you could use, since you need to cut of those milliseconds at exactly the same position each time.

However, this you would use when performing the query. If you cannot modify the query, due to it being automated in some way, you can add a step to the process and change the data first, then adding it to your database. A simple script would be able to read the csv, change it, write it back again, or perform the query directly.



来源:https://stackoverflow.com/questions/7904175/mysql-csv-import-date-entered-as-0000-00-00-000000-if-timestamp-has-millisec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!