2016-11-17 86 views
1

我正在使用python脚本将文本文件导入到mysql数据库,但我得到一个奇怪的错误并通过互联网搜索它,但无法找到确切的解决方案。我想创建列和某些列将与负号保存在十进制数据这样TypeError:在使用MySQL和Python进行字符串格式化时转换的并非所有参数

Alarmdata fuAlarm 
-1585.4  -35.3 
-343.32  -54.3 

我收到以下错误

_mysql_exceptions.ProgrammingError: (1064, "You have an error in your 
SQL syntax; check the manual that corresponds to your MySQL server 
version for the right syntax to use near '     Alarmdata, 
fuel_in_l DECIMAL(5,2), fuAlarm DECIMAL(4,3),  ' at line 1") 

我通过给数据类型为空白的数据,但现在解决了这个问题我正面临这个错误。

TypeError: not all arguments converted during string formatting 

我的代码如下

import os 
import sys 
import csv 
import MySQLdb as mdb 

con = mdb.connect('localhost' , 'root' , 'kami' , 'tempdat') 

cursor = con.cursor() 
cursor.execute('CREATE TABLE Datatmp (id INT NOT NULL PRIMARY KEY  AUTO_INCREMENT, \ 
      timestamp INT(12), pomode INT(3), modeAlarm INT(3), \ 
      temperature_C DECIMAL(2,1), temperatureAlarm INT(3), \ 
      switch INT(3), Powerswitch INT(3), SIM_dollar INT , \ 
      Alarmdata INT, fuel_in_l DECIMAL(5,2), fuAlarm DECIMAL(4,3), \ 
      next_maint DECIMAL(5,2), next_maintenanceAlarm INT(2))'); 

con.commit()    

file = open('//home/mysql/kami.txt', 'rb') 
creader = csv.reader(txtfile.readlines()[3:], delimiter='\t') 

for t in creader: 
    cursor.execute('INSERT INTO Datatmp(timestamp, pomode, modeAlarm,\ 
        temperature_C, temperatureAlarm, \ 
        switch, Powerswitch, SIM_dollar, \ 
        Alarmdata, fuel_in_l, fuAlarm \ 
        next_maint, next_maintenanceAlarm)\ 
        Values(%s,%s,%s,%s,%s,%s,NULL,NULL,%s,%s,%s,\ 
        %s,%s,%s,%s,%s,%s)', t) 

file.close() 
con.commit() 
con.close() 

类错误是说我没有通过足够%S,但我通过相同的号码,它不应该显示此错误。我是MySQL和python的新手,所以我没有解决。

+0

在您的表格定义中,您没有Alarmdata的数据类型 – Niagaradad

+0

@Niagaradad是的,那里我不知道。如果该列在表中,但没有数据,则只是空白。那么我应该显示哪种数据类型? NULL还是INT?第二,如果我有价值像-152.43,我该如何分配这种数据类型? 。如果有任何提示,我会非常感激。我花了整整一天的时间。 – Rio

+1

OP - 一个社区wiki被添加为您最后一个陈述的答案,但没有任何意义。 Python的* Nones *在MySQL中被翻译为* Nulls *。此外,您的附加查询缺少逗号,并且传入的列表字段的占位符太多。未来的读者可能会对你发现的内容感兴趣。请使用示例代码调整完全回答您自己的问题。 – Parfait

回答

0
import os 
import sys 
import csv 
import MySQLdb as mdb 


con = mdb.connect('localhost' , 'root' , 'kami' , 'tempdat') 

cursor = con.cursor() 
cursor.execute('CREATE TABLE Datatmp (timestamp INT(12), pomode INT(3), \ 
      modeAlarm INT(3), temperature_C DECIMAL(2,1),temperatureAlarm INT(3), \ 
      switch INT(3), Powerswitch INT(3),SIM_dollar INT(3), \ 
      Alarmdata INT(5),fuel_in_l DECIMAL(5,2), fuAlarm DECIMAL(4,3), \ 
      next_maint DECIMAL(5,2), next_maintenanceAlarm INT(2))'); 




con.commit()    

file = open('//home/mysql/kami.txt', 'rb') 
creader = csv.reader(txtfile.readlines()[3:], delimiter='\t') 



# now insert values in database 
i = 0; 

# read and store the values in database and also replace empty data 

for t in creader: 
    print t 
    for idx, v in enumerate(t): 
     if v == ' ': 
      t[idx] = None     
    cursor.execute('INSERT INTO Data_121_RAPI_txt VALUES(%s,%s,%s,%s,%s,%s, \ 
       %s,%s,%s,%s,%s,%s, %s)', t)     
i = i + 1 



file.close() 
con.commit() 
con.close() 

这里我面临两个问题。 1)如果有像Alarmdata这样的空列,我仍然需要传递数据类型,并且在这里传递INT数据类型,因此可以很容易地删除这个错误。

2) TypeError: not all arguments converted during string formatting  

这里的问题是,我通过NULL在cursor.execute和它给错误,所以我做的代码,把每一个空的空间,在里面写无。并且在palceholder中,我没有提供NULL,而只是普通的palceholder,并且提供了正确数量的占位符,问题就解决了。

我希望它能帮助别人。

0

在MySQL中,INT数据类型的值范围可以从-21474836482147483647。所以你可以简单地把数据类型INT放在那里。休息一切将由MySQL照顾。

+0

你的意思是指定-153.33我会给INT的数据类型?因为我通过分配INT数据类型来解决空白数据。虽然我得到一个新的错误。 TypeError:并非在字符串格式化过程中转换的所有参数 但我很快就会解决它。实际的问题是将数据类型分配给-153.33数字。 – Rio

0

(发表于OP)

我解决了这个问题。问题是,如果我们将它们转换为None类型,则Python会读取空格。

相关问题