mySQL加载数据本地infile不正确的数字格式

发布于 2024-12-12 00:41:14 字数 354 浏览 0 评论 0原文

我有一个像这样格式化的 csv 文件,

sth, sth, "100,000,000", sth, "200,000"
sth, sth, "200,000,000", sth, "500,000"

当我使用

mysql> load data local infile "thefile.csv" into table mytable terminated by ',' enclosed by '"' lines terminater by '\r\n';

时,“100,000,000”、“500,000”等数字会被截断。

请帮我!

I have an csv file formated like this,

sth, sth, "100,000,000", sth, "200,000"
sth, sth, "200,000,000", sth, "500,000"

When I used

mysql> load data local infile "thefile.csv" into table mytable terminated by ',' enclosed by '"' lines terminater by '\r\n';

then the numbers such as "100,000,000", "500,000" are truncated.

Please help me!

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

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

发布评论

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

评论(2

几度春秋 2024-12-19 00:41:14

从 CSV 文件中删除空格:

sth,sth,"100,000,000",sth,"200,000"
sth,sth,"200,000,000",sth,"500,000"

并尝试使用此语句加载数据 -

LOAD DATA INFILE 'thefile.csv' INTO TABLE mytable
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\r\n'
(column1, column2, @var1, column4, @var2) -- specify actual field names
SET column3 = REPLACE(@var1, ',', ''), column5 = REPLACE(@var2, ',', ''); -- remove thousand separators

Remove spaces from the CSV-file:

sth,sth,"100,000,000",sth,"200,000"
sth,sth,"200,000,000",sth,"500,000"

And try to use this statement to load data -

LOAD DATA INFILE 'thefile.csv' INTO TABLE mytable
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\r\n'
(column1, column2, @var1, column4, @var2) -- specify actual field names
SET column3 = REPLACE(@var1, ',', ''), column5 = REPLACE(@var2, ',', ''); -- remove thousand separators
原谅过去的我 2024-12-19 00:41:14

在 php 中尝试这种方法:

$in = fopen('filename.csv', 'r');
while (($row = fgetcsv($in, 10000, ',', '"')) !== false) {
  mysql_query("INSERT INTO ....");
}
fclose($in);

参考: fgetcsv

try this approach in php:

$in = fopen('filename.csv', 'r');
while (($row = fgetcsv($in, 10000, ',', '"')) !== false) {
  mysql_query("INSERT INTO ....");
}
fclose($in);

reference: fgetcsv

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