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.

Shani Gamrian
  • 345
  • 1
  • 4
  • 18

2 Answers2

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'])
Shani Gamrian
  • 345
  • 1
  • 4
  • 18
  • 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. – FlipperPA Jun 03 '16 at 13:54
  • 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. – kayla210 Jun 03 '16 at 14:15
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)
Ma0
  • 15,057
  • 4
  • 35
  • 65
  • I get the error: pypyodbc.ProgrammingError: (u'42S22', u"[42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'male'.") – Shani Gamrian Jun 03 '16 at 13:22