Skip to content Skip to sidebar Skip to footer

Attributeerror: 'tuple' Object Has No Attribute 'encode' - Mysqldb Python

I am writing a Python code with MySQL. My DB schema is as follows: ------------- | id | name | ------------- | | | | | | Following is a part of my code: cursor =

Solution 1:

The query parameters should be passed as a second parameter to execute():

cursor = self.conn.cursor() 
query = "SELECT name FROM TABLENAME WHERE id = %s"
cursor.execute(query, (str(id.decode('unicode_escape').encode('ascii', 'utf-8')), ))

Note that you don't need the single quotes around the %s placeholder - the database driver would put them automatically if needed depending on the query parameter type.

Post a Comment for "Attributeerror: 'tuple' Object Has No Attribute 'encode' - Mysqldb Python"