PyMySQL Incorrectly Identifying Variables as Tuple - python

I am trying to do an insert with PyMySQL in Python3, but I keep getting the following:
TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'
My code is as follows:
query = ("INSERT INTO table_name ",
"(type,sub) ",
"VALUES ",
"(%s,%s)")
#Execute the query
print("Category: %s" % category)
print("Category is string:",isinstance(category,str))
print("Category_group: %s" % category_group)
print("Category_group is string:",isinstance(category,str))
self.DB['master']['cursor'].execute(query,(category,category_group,))
self.DB['master']['con'].commit()
Which when running outputs:
Category: Amusement Park
Category is string: True
Category_group: Parks
Category_group is string: True
Traceback (most recent call last):
File "main.py", line 87, in <module>
m()
File "main.py", line 82, in __call__
self.deal.processdeal(deal=item,store=store)
File "file.py", line 85, in processdeal
btype = self.obtainBusinessType(deal['category'],deal['categoryGroup'])
File "file.py", line 59, in obtainBusinessType
self.DB['master']['cursor'].execute(query,(category,category_group,))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/cursors.py", line 130, in execute
query = query % self._escape_args(args, conn)
TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'
The output statements indicate that I am feeding strings as parameters, so why is it complaining about tuples?

Your problem is how you are building your query variable.
query = ("INSERT INTO table_name ",
"(type,sub) ",
"VALUES ",
"(%s,%s)")
print query
>>>('INSERT INTO table_name ', '(type,sub) ', 'VALUES ', '(%s,%s)')
query = query % ('hello', 'world')
TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'
Notice how your query is being outputted as a tuple, which causing the error.
You can use a str:
query = "INSERT INTO table_name (type,sub) VALUES (%s,%s)"
Or you remove the , from your query
query = ("INSERT INTO table_name "
"(type,sub) "
"VALUES "
"(%s,%s)")
Alternatively, as Kindall stated, you can also use triple-quote for multi-line strings
"""INSERT INTO table_name
(type,sub)
VALUES
(%s,%s)"""

Related

python postgresql TypeError: 'NoneType' object is not iterable for loop

i have an postgresql database
#p1 = {613309872640, 135117629824, 880993344004, 158175822853, 1073752495240, 216760058760, 447123298313, 912909533578, 762965475336, 306930570381, 972176171275, 6619746319, 343850096144, 764400555409, 546205930000, 528687305491, 404493333779, 77590592280, 855904184346, 936056534938, 661500784670, 141923831071, 562200894625, 567019890466, 832033035811, 72309584292, 364867195430, 190122917676, 144896917811, 580869171253, 1627505081, 627702833596, 644839868222, 172977438016, 1031295913668, 463277927492, 238807296070, 500185745223, 780490132936, 947594928202, 888064501323, 992713674444, 453125481684, 502841610453, 81933315933, 94767975264, 103246266465, 275027812832, 276760003301, 420374924396, 263072530544, 203425700337, 334072272115, 917262638965, 989106489206, 598614815351, 328241935477, 554909676025}
i use this code
connection = psycopg2.connect(user = "...",
password = "...",
host = "...",
port = "...",
database = "...")
c = connection.cursor()
.......
match_ids = set()
for chunk in p[1]:
for id in c.execute("SELECT S.val1 FROM table1 as F JOIN table2 AS S ON F.val2_id=S.val2_id WHERE F.val3=%s and F.val4=%s", (p[0], chunk)):
match_ids.add(id[0])
print(match_ids)
i can use this code on sqlite
but, I get an error when using postgresql
Error: TypeError: 'NoneType' object is not iterable
runcell(0, 'C:/Users/....../test1.py')
{613309872640, 135117629824, 880993344004, 158175822853, 1073752495240, 216760058760, 447123298313, 912909533578, 762965475336, 306930570381, 972176171275, 6619746319, 343850096144, 764400555409, 546205930000, 528687305491, 404493333779, 77590592280, 855904184346, 936056534938, 661500784670, 141923831071, 562200894625, 567019890466, 832033035811, 72309584292, 364867195430, 190122917676, 144896917811, 580869171253, 1627505081, 627702833596, 644839868222, 172977438016, 1031295913668, 463277927492, 238807296070, 500185745223, 780490132936, 947594928202, 888064501323, 992713674444, 453125481684, 502841610453, 81933315933, 94767975264, 103246266465, 275027812832, 276760003301, 420374924396, 263072530544, 203425700337, 334072272115, 917262638965, 989106489206, 598614815351, 328241935477, 554909676025}
Traceback (most recent call last):
File "C:\Users\.....\test1.py", line 208, in <module>
for id in c.execute("SELECT S.val1 FROM table1 as F JOIN table2 AS S ON F.val2_id=S.val2_id WHERE F.val3=%s and F.val4=%s", (p[0], chunk)):
TypeError: 'NoneType' object is not iterable
Try using replacing below snippet with your snippet.
for chunk in p[1]:
for id in c.execute("SELECT S.val1 FROM table1 as F JOIN table2 AS S ON F.val2_id=S.val2_id WHERE F.val3=%s and F.val4=%s", (p[0], chunk)) or []:
match_ids.add(id[0])
print(match_ids)
Actual issue seems that, query can't fetch the result.

Trouble inserting variable in sqlite python

Im having a problem inserting a variable into sqlite3 and the variable is hashed.
Here's my code:
if passadefinir == passadefinir2:
maindb.execute("DELETE FROM Password WHERE ID = 'not'")
maindb.execute("INSERT INTO Password(ID) VALUES ('set')")
encriptacao = hashlib.sha1(passadefinir2.encode())
encriptado = (encriptacao.hexdigest(),)
maindb.execute("INSERT INTO Password(Password) VALUES (?)" (encriptado,))
conn.commit()
Here's the error:
Traceback (most recent call last):
File "sqlitetesting.py", line 28, in
maindb.execute("INSERT INTO Password(Password) VALUES (?)" (encriptado,))
TypeError: 'str' object is not callable
Have a nice day :D,
Luis Duarte.
You can substitute using String's format method
password_var = 'your password'
insert_statement = 'INSERT INTO Password(Password) VALUES ({0})'.format('\'' + password_var + '\'')
then run the execute method as:
maindb.execute(insert_statement)

pymysql: MySQL Parameterized LIKE Query

Why am I getting TypeError: not all arguments converted during string formatting when trying to execute this query? I need to be able to append %{}% to the IP I am passing in so that i can run a LIKE mysql query.
If this isn't the correct way to parameterize a LIKE query using the % wildcard, how do you do this?
Class:
class IpCleaner(object):
def __init__(self, ip):
self.ip = ip
self.iplike = '%{}%'.format(self.ip)
def lookup(self):
self.dbconnect()
select_query = (
"SELECT `name`,`source`,`destination` FROM mytable "
"WHERE (`source` LIKE ? OR `destination` ? );"
)
params = [self.iplike, self.iplike]
print params
self.cur.execute(select_query, params)
print self.cur.fetchall()
Instantiation:
a = IpCleaner('74.121.242.2')
a.lookup()
output:
Traceback (most recent call last):
['%74.121.242.2%', '%74.121.242.2%']
File "/home/dobbs/shunlibs/IpCleaner.py", line 87, in <module>
a.palorulelookup()
File "/home/dobbs/shunlibs/IpCleaner.py", line 81, in lookup
self.cur.execute(select_query, params)
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
use %s instead of ? in the select_query variable

Error inserting value with special character into mysql table using python

Having an issue inserting a value into a mysql table using a dynamic list. I keep having a problem with the quotes pointing to a table instead of a value.
Here is the code:
lst = ['Pid', 'Base', 'Size', 'LoadCount', 'Path','Casename']
lst2 =['888', '1213726720', '61440', '65535', '\\SystemRoot\\System32\\smss.exe', 'wean']
table_name ="test"
insert_intotable = "INSERT INTO " + table_name + "(" + ",".join(lst) + ") VALUES (" + ",".join(lst2) + ")"
print insert_intotable
c.execute(insert_intotable)
conn.commit()
c.close()
conn.close()
This causes the following error:
> Traceback (most recent call last): File "pp.py", line 53, in
> <module>
> c.execute(insert_intotable) File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in
> execute
> self.errorhandler(self, exc, value) File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in
> defaulterrorhandler
> raise errorclass, errorvalue
> _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
> '\\SystemRoot\\System32\\smss.exe,test)' at line 1")
What is causing this syntax issue?
Use 'single quotes' when inserting string values
UPD:
Change second line to this
lst2 =['888', '1213726720', '61440', '65535', '\'\\SystemRoot\\System32\\smss.exe\'', '\'wean\'']
Because, it is wrong SQL query:
INSERT INTO test (Path) VALUES(\SystemRoot\System32\smss.exe)
And your code generates such query.
You should quote varchar values:
INSERT INTO test (Path) VALUES('\SystemRoot\System32\smss.exe')

Impossible to insert data with PyMySQL when I use parameter

I'm currently working on a basic query which insert data depending on the input parameters, and I'm unable to perfom it.
cur.execute("INSERT INTO foo (bar1, bar2) values (?, ?)", (foo1, foo2))
I have this error message:
Exception in Tkinter callback Traceback (most recent call last):
File "/usr/lib/python3.2/tkinter/init.py", line 1426, in call
return self.func(*args) File "test.py", line 9, in register
cur.execute("INSERT INTO foo (bar1, bar2) values (?,?)", (foo1, foo2)) File
"/usr/local/lib/python3.2/dist-packages/pymysql/cursors.py", line 108,
in execute
query = query % escaped_args TypeError: unsupported operand type(s) for %: 'bytes' and 'tuple'
foo1 and foo2 are both string type. I tried with %s, same error.
It seems like a bug in cursors.py. As suggested here and here you should replace this line in cursors.py:
query = query % conn.escape(args)
With this:
query = query.decode(charset) % conn.escape(args)
In case it didn't work try this one instead:
query = query.decode(charset) % escaped_args

Categories

Resources