3

I use Python 2.7 and pypyodbc in order to run SQL queries but whenever I run the update query using python,

cursor.execute("UPDATE tbl_User SET gender = ? WHERE id = 1", ['male'])

I get the error:

TypeError: string or integer address expected instead of instance instance

The same query works if I run it directly on SQL Server.

4
  • Are you able to get it to work without passing the parameter and hard coding it into the UPDATE statement? Commented Jun 3, 2016 at 13:17
  • you mean something like that: cursor.execute("UPDATE tbl_User SET gender = 'male' WHERE id = 1")? Commented Jun 3, 2016 at 13:18
  • Yes, just to ensure the code is executable through pypyodbc. :) Commented Jun 3, 2016 at 13:23
  • I get the error :/ but as I said, it does work if I run it directly on SQL Server Commented Jun 3, 2016 at 13:27

2 Answers 2

3

The problem was I didn't put a semicolon at the end of the query. The solution is:

cursor.execute("UPDATE tbl_User SET gender = ? WHERE id = 1;", ['male'])
Sign up to request clarification or add additional context in comments.

2 Comments

You may want to amend your answer and tag it to include what database you're using, since this would seem to be database specific.
Ah, that makes sense. SQL Server's really adamant about those semi-colons, but it'd been a while since I've used it, so that didn't pop out to me.
0

How about that:

gender = 'male'
cursor.execute("UPDATE tbl_User SET gender = {:s} WHERE id = 1".format(gender))

Also make sure that the python version of sqlite and the one used by the SQL Server are the same. For the python one type:

import sqlite3
print(sqlite3.sqlite_version)

1 Comment

I get the error: pypyodbc.ProgrammingError: (u'42S22', u"[42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'male'.")

Your Answer

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.