I have the following Python code:
cursor = connection.cursor()
a = "C6DE6778-5956-48D4-BED6-5A2A37BBB123"
SQLCommand = ("""SELECT *
FROM Table
WHERE Table.ENUM = ?
""", a)
results = cursor.execute(SQLCommand)
The following error is returned:
TypeError: string or integer address expected instead of tuple instance
asked Apr 6, 2017 at 15:46
Ekaterina
3351 gold badge4 silver badges11 bronze badges
-
What library are you using to connect to your database? It may not support Prepared Statements like that.Darney– Darney2017年04月06日 15:51:27 +00:00Commented Apr 6, 2017 at 15:51
-
@Darnell Martin pypyodbcEkaterina– Ekaterina2017年04月06日 15:59:05 +00:00Commented Apr 6, 2017 at 15:59
-
1I think it should work : ``` a = "C6DE6778-5956-48D4-BED6-5A2A37BBB123" sql = "SELECT * FROM Table WHERE Table.ENUM = %s" cursor.execute(sql, a) ```Sijan Bhandari– Sijan Bhandari2017年04月06日 16:05:22 +00:00Commented Apr 6, 2017 at 16:05
2 Answers 2
The way you constructed the sqlcommand is incorrect. Pass the parameter when you execute.
a = "C6DE6778-5956-48D4-BED6-5A2A37BBB123"
SQLCommand = """SELECT *
FROM Table
WHERE Table.ENUM = ?
"""
results = cursor.execute(SQLCommand,(a,))
answered Apr 6, 2017 at 16:03
Vamsi Prabhala
49.4k4 gold badges41 silver badges64 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
SQLCommand is a tuple in your case. .execute() expects sql statement as the first argument. To rectify the error, you can do something like this :
cursor = connection.cursor()
a = "C6DE6778-5956-48D4-BED6-5A2A37BBB123"
SQLCommand = """SELECT *
FROM Table
WHERE Table.ENUM = '%s'
""" % a
results = cursor.execute(SQLCommand)
Alternatively, you can format you SQL statement string like this :
SQLCommand = """SELECT *
FROM Table
WHERE Table.ENUM = '{}'
""".format(a)
Or you can pass a as an optional parameter to .execute() like this :
cursor = connection.cursor()
a = "C6DE6778-5956-48D4-BED6-5A2A37BBB123"
SQLCommand = """SELECT *
FROM Table
WHERE Table.ENUM = ?
"""
print(SQLCommand, a)
You can refer to the documentation for more understanding on this.
answered Apr 6, 2017 at 16:06
Satish Prakash Garg
2,2232 gold badges19 silver badges26 bronze badges
Comments
default