pyodbc column name as variable

独自空忆成欢 提交于 2019-12-12 02:29:45

问题


Instead of editing the sql-string when i want to change which column to write to i want to use a variable.. This seems to work. I get no error, but the database is not updated... What is wrong with this?

cnxn = pyodbc.connect('DRIVER=FreeTDS;SERVER=dkvmsql08;PORT=1433;DATABASE=dingdong_test;UID=dac\svc-dingdong;PWD=c90346KJHjkhg&%dad742210a3d6fd4436c;T$
cur = cnxn.cursor()

name = Dennis
cur.execute("UPDATE dbo.timestamp  set cur.execute("UPDATE dbo.test  set ?=1 where id=?",name, row.id)

回答1:


There's no explicit commit statement in your code and the connect call doesn't include autocommit=True, so try adding cnxn.commit() to the end of your code. :)




回答2:


Query parameters can be used to specify the values of columns but not the column names, so you would need to do something like the following:

name = "yourColumnName"
sql = "UPDATE dbo.timestamp SET [{}]=1 WHERE ID=?".format(name)
cur.execute(sql, row.id)
cur.commit()


来源:https://stackoverflow.com/questions/21858012/pyodbc-column-name-as-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!