TypeError: not all arguments converted during string formatting, I have this error
I want to generate a query to remove data from my database, but I get the following error: TypeError: not all arguments have been converted during string formatting
. Where am I going wrong?
def delete(name):
conn = psycopg2.connect(dbname="postgres", user="postgres",
password="password", host="localhost", port="5432")
cursor = conn.cursor()
query = '''DELETE FROM products WHERE name IN {%s} '''
cursor.execute(query, [name])
conn.commit()
conn.close()
Solution 1:
Both of {}
and %s
are formatting placeholders in psycopg2
, see docs. Using them both at once means no sense. You can fix it by replacing this:
query = '''DELETE FROM products WHERE name IN {%s} '''
with this:
query = '''DELETE FROM products WHERE name IN %s '''
Also, I'm not sure if it changes anything, but the second argument for cursor.execute
has to be a tuple, not a list. Consider replacing cursor.execute(query, [name])
with cursor.execute(query, (name,))
.