UPDATE语句pyodbc在sql server中设置NULL值(而不是None) [英] UPDATE statement pyodbc set NULL value (instead of None) in sql server
本文介绍了UPDATE语句pyodbc在sql server中设置NULL值(而不是None)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有代码要更新数据库中的某些条目.某些属性可以具有 NULL
值.
I have code where I want to update certain entries in a database.
Some attributes can have a NULL
value.
让我们看看以下情况:
new_doc_name = doc_name (can be None or string value)
sql = UPDATE Documents SET DocName = new_doc_name WHERE DocID = doc_id
cursor.execute(sql);
cursor.commit()
当new_doc_name为None
时,数据库中的值设置为None
,但我希望将其设置为NULL
.
When new_doc_name is None
, the value in the database is set as None
, but I want it to be set as NULL
.
对 INSERT
执行相同操作时,值设置为 NULL
而不是我想要的 None
.但是,我无法使用 UPDATE
语句复制它.
When doing the same for INSERT
, the values are set as NULL
instead of None
, which I want. I can't, however, replicate this with the UPDATE
statement.
推荐答案
您需要使用参数化查询:
You'll want to use a parameterized query:
sql = "UPDATE Documents SET DocName = ? WHERE DocID = ?"
cursor.execute(sql, new_doc_name, doc_id);
这篇关于UPDATE语句pyodbc在sql server中设置NULL值(而不是None)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文