Python pymysql:TypeError:在字符串格式化期间并非所有参数都已转换
我用头撞这堵墙已经有一段时间了。我无法使此插入查询正常工作。是因为我插入了多个数据类型(int、bool、strings、NoneType)并且使用了Python pymysql:TypeError:在字符串格式化期间并非所有参数都已转换,python,mysql,pymysql,Python,Mysql,Pymysql,我用头撞这堵墙已经有一段时间了。我无法使此插入查询正常工作。是因为我插入了多个数据类型(int、bool、strings、NoneType)并且使用了%s?任何帮助都将不胜感激。谢谢 class DBWork(object): def __init__(self, attackerip=None, victimip=None, shunlen=15, shuncount=1, shunreason=None, shunepoch=None, shuner
%s
?任何帮助都将不胜感激。谢谢
class DBWork(object):
def __init__(self, attackerip=None, victimip=None, shunlen=15, shuncount=1, shunreason=None, shunepoch=None,
shunerrorbool=False, shunerror=None, paloaddedepoch=None, paloremovedepoch=None,
lastshunlen=15):
self.Id = None
self.attackerip = attackerip
self.victimip = victimip
self.shunlen = shunlen
self.shuncount = shuncount
self.shunreason = shunreason
self.shunepoch = shunepoch
self.shundatetime = 'need to build this'
self.shunerrorbool = shunerrorbool
self.shunerror = shunerror
self.paloaddedepoch = paloaddedepoch
self.paloremovedepoch = paloremovedepoch
self.lastshunlen = lastshunlen
self.shunlen = shunlen
self.newshunlen = lastshunlen * shuncount
def HistoryInsert(self):
insert_query = (
"INSERT INTO `autoshun`.`shunhistory` "
"(Id, attackerip, victimip, shunlen, shuncount, shunreason, shunepoch, shundatetime, shunerrorbool, "
"shunerror, paloaddedepoch, paloremovedepoch) "
"VALUES "
"(NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s );"
)
values = [None, self.attackerip, self.victimip, self.shunlen, self.shuncount, self.shunreason, self.shunepoch,
self.shundatetime, self.shunerrorbool, self.shunerror, self.paloaddedepoch, self.paloremovedepoch]
print values
self.cur.execute(insert_query, values)
sample = DBWork(attackerip='1.1.1.15', victimip='2.2.2.2', shunlen=15, shuncount=1, shunreason='hax', shunerrorbool=True)
sample.DBConnect()
sample.HistoryInsert()
正在传入的值:
values=[None,'1.1.1.15','2.2.2',15,1,'hax','NULL','needtobuildthis','False','NULL','NULL','NULL']
错误:
Traceback (most recent call last):
File "/home/dobbs/PycharmProjects/autoshun/sqlobjects.py", line 102, in <module>
sample.HistoryInsert()
File "/home/dobbs/PycharmProjects/autoshun/sqlobjects.py", line 95, in HistoryInsert
self.cur.execute(insert_query, values)
File "/usr/lib64/python2.7/site-packages/pymysql/cursors.py", line 164, in execute
query = self.mogrify(query, args)
File "/usr/lib64/python2.7/site-packages/pymysql/cursors.py", line 143, in mogrify
query = query % self._escape_args(args, conn)
TypeError: not all arguments converted during string formatting
回溯(最近一次呼叫最后一次):
文件“/home/dobbs/PycharmProjects/autoshun/sqlobjects.py”,第102行,在
sample.HistoryInsert()
文件“/home/dobbs/PycharmProjects/autoshun/sqlobjects.py”,第95行,在HistoryInsert中
self.cur.execute(插入查询、值)
文件“/usr/lib64/python2.7/site packages/pymysql/cursors.py”,执行中的第164行
query=self.mogrify(query,args)
文件“/usr/lib64/python2.7/site packages/pymysql/cursors.py”,第143行,在mogrify中
查询=查询%self.\u转义\u参数(参数,conn)
TypeError:在字符串格式化过程中并非所有参数都已转换
继续,省去id
字段。MySQL将知道该做什么:
insert_query = (
"INSERT INTO `autoshun`.`shunhistory` "
"(attackerip, victimip, shunlen, shuncount, shunreason, shunepoch, shundatetime, shunerrorbool, "
"shunerror, paloaddedepoch, paloremovedepoch) "
"VALUES "
"(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s );"
)
values = [self.attackerip, self.victimip, self.shunlen, self.shuncount, self.shunreason, self.shunepoch,
self.shundatetime, self.shunerrorbool, self.shunerror, self.paloaddedepoch, self.paloremovedepoch]
按原样,您的查询有一个
NULL
,您正在尝试传入None
,但没有该None
的参数,谢谢!!当然了,干杯,伙计。很高兴见到你!