Reputation: 297
I'm trying to put actively some datas from a list to my database in the table 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:
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:
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'
Upvotes: 2
Views: 1454
Reputation: 1125028
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?
Upvotes: 9