[英]TypeError: not all arguments converted during string formatting using MySQL and Python
I am importing text file to mysql database by using python script, but I get a weird error and search for it over internet but could'nt find exact solution. 我使用python脚本将文本文件导入mysql数据库,但我得到一个奇怪的错误并通过互联网搜索它但无法找到确切的解决方案。 I want to create columns and some columns will store data in decimal with negative sign like this 我想创建列,一些列将以十进制数字存储数据,如下所示
Alarmdata fuAlarm
-1585.4 -35.3
-343.32 -54.3
I get the following error 我收到以下错误
_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")
I solved this issue by giving data type to blank data but now I am facing this error. 我通过将数据类型提供给空白数据解决了这个问题,但现在我遇到了这个错误。
TypeError: not all arguments converted during string formatting
My code is below 我的代码如下
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()
Type error mean that I didn't pass enough %s , but I passed the same number and it should not display this error. 类型错误意味着我没有传递足够的%s,但我传递了相同的数字,它不应该显示此错误。 I am new to MySQL and python as well, so I didn't solve. 我也是MySQL和python的新手,所以我没有解决。
In MySQL, INT datatype can have the value ranges from -2147483648
to 2147483647
. 在MySQL中,INT数据类型的值范围为-2147483648
到2147483647
。 So you can simply put the datatype INT there. 所以你可以简单地把数据类型INT放在那里。 Rest everything will be by taken care by MySQL. 休息一切都将由MySQL照顾。
(Posted on behalf of the OP) . (代表OP发布) 。
I solved this problem. 我解决了这个问题。 The issue is that blank spaces are read by Python if we convert them to type of None. 问题是如果我们将它们转换为None类型,则Python会读取空格。
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()
Here I face two problems. 在这里,我面临两个问题。 1) if there are empty columns like Alarmdata, I still need to pass datatype and here I pass INT datatype so this error can easily be removed. 1)如果有像Alarmdata这样的空列,我仍然需要传递数据类型,这里我传递INT数据类型,因此可以很容易地删除此错误。
2) TypeError: not all arguments converted during string formatting
Here the problem was that I pass NULL in cursor.execute and it gives error, so I make code which turn every empty space and write there None. 这里的问题是我在cursor.execute中传递了NULL并且它给出了错误,因此我创建了转换每个空白空间并写入None的代码。 and in palceholder I didn't provide NULL but simply normal palceholder, and also providing correct number of placeholders , the issue is solved. 在palceholder中我没有提供NULL,只是普通的掌上电脑,并且还提供了正确数量的占位符,问题就解决了。
I hope it will help people. 我希望它能帮到人们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.