pyodbc 列名作为变量 [英] pyodbc column name as variable

查看:67
本文介绍了pyodbc 列名作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想更改要写入的列时,我不想编辑 sql 字符串,而是想使用变量.这似乎有效.我没有收到错误,但数据库没有更新......这有什么问题?

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)

推荐答案

查询参数可用于指定列的而不是列的名称,因此您需要执行以下操作:

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

这篇关于pyodbc 列名作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆