Saturday, 15 August 2015

sql server - an error of running a SQL query from cursor.execute in pyodbc of python 3.2.5 onm win 7 -



sql server - an error of running a SQL query from cursor.execute in pyodbc of python 3.2.5 onm win 7 -

i working on pyodbc in eclipse (4.3.2v20140221-1852) pydev on win 7. python 3.2.5.

at code:

cursor.execute("select top " + str(1) + " a.my_id, a.mycode" + "from my_table a.mycode = ?", agivencode)

i got error :

pyodbc.programmingerror: ('42000', "[42000] [microsoft][odbc sql server driver] [sql server]incorrect syntax near keyword 'as'. (156) (sqlexecdirectw)")

why got error @ " " ?

thanks !

a couple suggestions future development:

use multi-line string notation """ allow improve identification of syntax errors (like missing space between column , table in original query).

instead of string concatenation, utilize parameter top values. value must wrapped in parentheses.

with these guidelines, original code updated to:

top = 1 cursor.execute("""select top (?) a.my_id, a.mycode my_table a.mycode = ?""", (top, agivencode))

sql-server sql-server-2008 python-3.x windows-7 pyodbc

No comments:

Post a Comment