MySQL CSV 导入 - 如果时间戳有毫秒,则日期输入为 0000-00-00 00:00:00?

发布于 2024-12-11 21:46:01 字数 707 浏览 0 评论 0原文

我目前有大量 CSV 需要导入到 MySQL 数据库中。这些文件包含每条记录的时间戳,其格式如下(例如):

2011-10-13 09:36:02.297000000

我知道 MySQL 错误 #8523,这表明不支持在日期时间字段中存储毫秒。尽管如此,我还是希望日期时间字段在几秒后截断记录,而不是输入空白。

我已将问题范围缩小到毫秒(而不是 csv 的格式等)

2011-10-13 09:36:02

由于导入正确,

。谁能建议一种方法,让我可以导入没有零的数据?我有太多 CSV,无法手动进入每个 CSV 并调整时间戳的长度/格式。

我应该指出,虽然毫秒是一个很好的选择,但它们对于我的应用程序来说并不是必需的,因此我会很高兴有一个解决方案可以让我轻松截断数字并导入它们。

谢谢!

编辑:为了澄清,我使用以下命令导入 CSV:

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

这对于导入记录来说非常快 - 我有大约 50m 需要导入,所以读取每一行并不是一个很好的选择。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

像极了他 2024-12-18 21:46:01

我不知道你如何导入 CSV,但我要做的方法是编写一个脚本(php/perl)来读取每个文件,将时间戳四舍五入或修剪为秒,然后在数据库上执行 INSERT 语句。

一样,它应该可以完成工作

<?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);
?>

像从命令行执行这个

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

柒七 2024-12-18 21:46:01

使用毫秒时它不会导入,但不使用毫秒时它会导入。所以你必须以一种或另一种方式进行子串。 MySQL 有各种字符串函数,例如 SUBSTRING - 您可以使用它,因为您每次都需要在完全相同的位置削减这些毫秒。

但是,您将在执行查询时使用它。如果您无法修改查询,因为它以某种方式自动化,您可以向流程添加一个步骤并首先更改数据,然后将其添加到数据库中。一个简单的脚本就能够读取 csv、更改它、再次写回它或直接执行查询。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文