0

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
3
  • What library are you using to connect to your database? It may not support Prepared Statements like that. Commented Apr 6, 2017 at 15:51
  • @Darnell Martin pypyodbc Commented Apr 6, 2017 at 15:59
  • 1
    I think it should work : ``` a = "C6DE6778-5956-48D4-BED6-5A2A37BBB123" sql = "SELECT * FROM Table WHERE Table.ENUM = %s" cursor.execute(sql, a) ``` Commented Apr 6, 2017 at 16:05

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.