问题
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