2017-03-07 22 views
0

我试图将文本文件导入到mysql数据库中。首先,我创建的模式: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名最高windspeeds)这就是结果:

|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

其实'decimal'变得圆了。你能把这个字段改成“浮动”吗?我还在想。 – jiveturkey

+0

只是将模式更改为浮动模式并且工作。不知道为什么,但你现在是1/1。你能修好年份专栏吗? – alexs973

+0

是的,这很奇怪。 'INT'默认为'INT(11)',但尝试给它一个明确的限制 - INT(5)' – jiveturkey

回答

0

我找到了解决方案。我的原始.txt文件是在Windows中创建的。然后我将它(通过WinSCP)导入到我的Linux机器中。我发现Linux对字符做了一件奇怪的事情,有时Linux和微软之间的字符转换可能会有些粗糙。所以为了测试这个理论,我在Linux中打开了一个新的vi,并复制并粘贴原始.txt文件中的数据。然后我使用新的vi文件将数据加载到我的表中。它的工作,没有错误。