0

I am writing the following query:

x1 = 5
y1 = 6
z1 = 0
SQL = ("SELECT Z FROM Points WHERE X =x1 AND Y = y1")
    for row in cursor.execute(SQL):
        z1 = row.Zcor

I am getting the following error:

Traceback (most recent call last): File "../ppp.py", line 38, in for row in cursor.execute(SQLp1z): pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][\xc4\xf0\xe0\xe9\xe2\xe5\xf0 ODBC Microsoft Access] \xcd\xe5\xee\xef\xf0\xe5\xe4\xe5\xeb\xe5\xed\xed\xe0\xff \xf4\xf3\xed\xea\xf6\xe8\xff 'convert' \xe2 \xe2\xfb\xf0\xe0\xe6\xe5\xed\xe8\xe8. (-3102) (SQLExecDirectW)")

What's the error in my code and what do I need to do in order to prevent this from happening ?

Bugs
  • 4,491
  • 9
  • 32
  • 41
Narek Tarasyan
  • 441
  • 1
  • 4
  • 5
  • is your spacing off for the for loop or is that just how you copied it here? – John Smith Mar 08 '17 at 16:37
  • take a look at this, it looks like you're not including variables in your query string properly. http://stackoverflow.com/questions/902408/how-to-use-variables-in-sql-statement-in-python – Will Mar 08 '17 at 16:39

1 Answers1

0

Your problem is that you are not including variables in your string correctly. Use string formatting:

SQL = ("SELECT Z FROM Points WHERE X ={x1} AND Y ={y1}".format(x1=x1, y1=y1)

Or in python 3.6:

SQL = (f"SELECT Z FROM Points WHERE X ={x1} AND Y ={y1}")
rassar
  • 5,412
  • 3
  • 25
  • 41