我正在尝试将文本文件导入mysql数据库。首先我创建了模式:
Create table tbl_windspeed(
year int,
month varchar(50),
speed decimal,
location varchar(50));
这是txt文件的一部分:
2015 feb 15.7 Neumayer
2015 sep \N Neumayer
2015 nov \N Neumayer
2015 jun 17.3 Neumayer
1976 jul \N Rothera
1976 may \N Rothera
1976 oct \N Rothera
1976 apr \N Rothera
1976 mar \N Rothera
1976 aug \N Rothera
1976 jan \N Rothera
这是import语句和上面相同的行:
LOAD DATA INFILE 'csv_windspeed.txt' INTO TABLE tbl_windspeed;
|015 | feb | 16 | Neumayer
|015 | sep | NULL | Neumayer
|015 | nov | NULL | Neumayer
|015 | jun | 17 | Neumayer
|76 | jul | NULL | Rothera
|76 | may | NULL | Rothera
|76 | oct | NULL | Rothera
|76 | apr | NULL | Rothera
|76 | mar | NULL | Rothera
|76 | aug | NULL | Rothera
|76 | jan | NULL | Rothera
所以有两件事:
1)年份整数被切断
2)即使在表模式中指定了风速数据,它们也不会被读取为小数。
有趣的是,当我运行查询时:
SELECT * FROM tbl_windspeed WHERE year = 1962
我得到了这个结果:
|62 | nov | 8 | Faraday
|62 | jun | 11 | Faraday
|1962 | jul | 11 | Grytviken
|1962 | may | 7 | Grytviken
|1962 | oct | 6 | Grytviken
|1962 | apr | 7 | Grytviken
|1962 | mar | 12 | Grytviken
|1962 | aug | 7 | Grytviken
|1962 | jan | 9 | Grytviken
|1962 | dec | 8 | Grytviken
|1962 | feb | 9 | Grytviken
|1962 | sep | 9 | Grytviken
|1962 | nov | 12 | Grytviken
|1962 | jun | 9 | Grytviken
|2 | jul | 13 | Halley
|2 | may | 9 | Halley
|2 | oct | 11 | Halley
|2 | apr | 13 | Halley
|2 | mar | 11 | Halley
|2 | aug | 10 | Halley
|2 | jan | 8 | Halley
|2 | dec | 9 | Halley
|2 | feb | 11 | Halley
|2 | sep | 14 | Halley
|2 | nov | 9 | Halley
|2 | jun | 11 | Halley
| | jul | 14 | Signy
| | may | 11 | Signy
| | oct | 19 | Signy
| | apr | 15 | Signy
| | mar | 11 | Signy
| | aug | 12 | Signy
| | jan | 11 | Signy
| | dec | 11 | Signy
| | feb | 16 | Signy
| | sep | 18 | Signy
| | nov | 13 | Signy
| | jun | 16 | Signy
所以尽管mysql意识到这些行是从1962年开始的,但它们仍然被切断了。同样,小数也被切断了。我不确定这里发生了什么,感谢任何帮助,谢谢。
更新:当我在mysql中运行查询(获得前10个最高风速)时,这就是结果:
|1988 | jun | 27.9 | Neumayer
|1997 | nov | 26.9 | Neumayer
|1981 | jul | 26.8 | Neumayer
|2002 | jun | 25.8 | Neumayer
|2006 | aug | 25.6 | Neumayer
|1989 | apr | 25.5 | Neumayer
|1995 | aug | 25.5 | Neumayer
|1981 | may | 25.4 | Neumayer
|1999 | aug | 25 | Neumayer
|8 | sep | 25 | Signy
请注意,最后一行的年份列被截断。现在当我运行一个运行同一个查询的C程序时,结果是:
988 jun 27.9 Neumayer
997 nov 26.9 Neumayer
981 jul 26.8 Neumayer
002 jun 25.8 Neumayer
006 aug 25.6 Neumayer
989 apr 25.5 Neumayer
995 aug 25.5 Neumayer
981 may 25.4 Neumayer
999 aug 25 Neumayer
958 sep 25 Signy
我认为它必须与视图设置有关?它是相同的查询,相同的数据,但有时年份列被切断。
答案 0 :(得分:0)
我找到了解决方案。我原来的.txt文件是在windows中创建的。然后我将它(通过WinSCP)导入我的Linux机器。我发现Linux对于字符做了一件奇怪的事情,有时Linux和Microsoft之间的字符转换可能有点粗糙。因此,为了测试这个理论,我在Linux中开辟了一个新的vi,并复制并粘贴了原始.txt文件中的数据。然后我使用新的vi文件将数据加载到我的表中。它工作,没有错误。