2017-03-08 21 views
0

我寫了下面的查詢:從SQL Python的選擇得到錯誤

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 

我收到以下錯誤:

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)")

是什麼在我的代碼錯誤,什麼我需要做的爲了防止這種情況發生?

+0

是您的間隔關閉for循環或者是多麼你在這裏複製呢? –

+0

看看這個,看起來你沒有正確地在你的查詢字符串中包含變量。 http://stackoverflow.com/questions/902408/how-to-use-variables-in-sql-statement-in-python – Will

回答

0

你的問題是你沒有正確地在你的字符串中包含變量。使用字符串格式化:

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

或者在Python 3.6:

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