[英]ProgrammingError: Wrong number of arguments during string formatting
I'm trying to put actively some datas from a list to my database in the table polls_ip
. 我正在尝试将列表中的一些数据主动放入表
polls_ip
数据库中。 However it seems to be some problems of arguments maybe owing the type of the data I want to put inside. 然而,似乎存在一些争论的问题可能是由于我想要放入的数据类型。
If you want to have a look of the usefull part of my code: 如果你想看看我的代码中有用的部分:
fin_flag = ( tcp.flags & dpkt.tcp.TH_FIN ) != 0
rst_flag = ( tcp.flags & dpkt.tcp.TH_RST ) != 0
if fin_flag or rst_flag:
src_ip2 = socket.inet_ntoa(ip.src)
dst_ip2 = socket.inet_ntoa(ip.dst)
for element in liste:
if element == (src_ip2+" "+dst_ip2) or element == (dst_ip2+" "+src_ip2):
liste.remove(element)
cursor.execute("INSERT INTO polls_ip (Ip) VALUES (%s)", (element))
The problem probably come from the line inside cursor.execute
Have a look at the output: 问题可能来自
cursor.execute
里面的行看看输出:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "scriptbdd.py", line 134, in run
self.p.dispatch(0, PieceRequestSniffer.cb)
File "scriptbdd.py", line 120, in cb
cursor.execute("INSERT INTO polls_ip (Ip) VALUES (%s)", (element))
File "/usr/lib/pymodules/python2.7/mysql/connector/cursor.py", line 310, in execute
"Wrong number of arguments during string formatting")
ProgrammingError: Wrong number of arguments during string formatting
And even with a coma in (element,))
, another problem is raised: 即使在
(element,))
出现昏迷,也会出现另一个问题:
root@debian:/home/florian/Documents/mysite/polls# python scriptbdd.py
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "scriptbdd.py", line 132, in run
self.p.dispatch(0, PieceRequestSniffer.cb)
File "scriptbdd.py", line 120, in cb
cursor.execute("INSERT INTO polls_ip (Ip) VALUES (%s)", (element,))
File "/usr/lib/pymodules/python2.7/mysql/connector/cursor.py", line 314, in execute
res = self.db().protocol.cmd_query(stmt)
InterfaceError: Failed executing the operation; 'NoneType' object has no attribute 'cmd_query'
You forgot a comma: 你忘了逗号:
cursor.execute("INSERT INTO polls_ip (Ip) VALUES (%s)", (element,))
# add a comma here ^
That makes that second argument a tuple with one element, rather than just one value. 这使得第二个参数成为具有一个元素的元组,而不仅仅是一个值。
If you find it easier to work with, you could make that second argument a list rather than a tuple: 如果您发现它更容易使用,您可以将第二个参数设为列表而不是元组:
cursor.execute("INSERT INTO polls_ip (Ip) VALUES (%s)", [element])
Tuples are formed by using a comma (and parentheses are only really required when disambiguating those commas from other uses), while lists are formed by using the [..]
syntax. 元组是使用逗号形成的(只有在消除其他用途的逗号时才需要括号),而列表是使用
[..]
语法形成的。
Your second error indicates your database connection has been closed; 您的第二个错误表明您的数据库连接已关闭; you cannot pass a cursor around if your database connection was stored in a local somewhere which has been cleaned up.
如果数据库连接存储在已清理的本地某处,则无法传递光标。 For example, you cannot create a database connection in a function and only return the cursor;
例如,您无法在函数中创建数据库连接并仅返回游标; the local variable that references the connection would be cleaned up and the connection would close before you could use the cursor.
在您使用游标之前,将清除引用连接的局部变量并关闭连接。 See Why won't Python return my mysql-connector cursor from a function?
请参阅为什么Python不会从函数返回我的mysql-connector游标?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.