简体   繁体   English

pyodbc参数化查询的语法错误

[英]Syntax error for parameterized query with pyodbc

I'm new to Python (used to C#) and need to use an Access database (.accdb) with it. 我是Python的新手(用于C#),需要与它一起使用Access数据库(.accdb)。

The syntax for building SQL Queries is also a bit odd for me. 构建SQL查询的语法对我来说也有点奇怪。

I have the following: 我有以下内容:

def updateSQL(table,keyField,keyVal,field,newVal):
    sqlCommand = "UPDATE " + table + " SET (?)=(?) WHERE (?)=(?);"
    crsr.execute(sqlCommand, (field, newVal, keyField, keyVal))
    crsr.commit()
    print("Tables update successfully")

But for some reason I'm getting the following error: 但是由于某种原因,我收到以下错误:

[Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement [Microsoft] [ODBC Microsoft Access驱动程序] UPDATE语句中的语法错误

I've tried a few different things with the statement and I can't for the life of me work out where it's going wrong, any ideas? 我在声明中尝试了几种不同的方法,但我无法为自己的生活弄清楚哪里出了问题,有什么想法吗?

The '?' '?' markers are for values , but the column name is not a value. 标记用于 ,但列名不是值。 You never want to put values into your SQL, but you will need put the columns. 您从不希望将值放入SQL中,但是您将需要放入列。 Try something like: 尝试类似:

sql = 'update {} set {}=? where {}=?'.format(table, field, keyField)
cursor.execute(sql, newVal, keyVal)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM