我需要删除DB项,其中值可以是无。我从一个ListBox我的价值观。功能如下:
def OnDelete(self, e):
num = self.list.GetItemCount()
for i in range(num):
if self.list.IsChecked(i):
itemValueList = []
for i2 in range(6):
item = self.list.GetItem(i, i2)
if item.GetText() == "None":
itemValueList.append("")
else:
itemValueList.append(item.GetText())
DBManager.DeleteEntry(itemValueList)
而在我DBManager的功能如下:
def DeleteEntry(itemValueList):
# Open Database
conn = sqlite3.connect('Database.db')
c = conn.cursor()
# Delete a row of data
c.execute('delete from Database where Value1=? and Value2=? and Value3=? and Value4=? and Value5=? and Value6=?',
(itemValueList[0], itemValueList[1], itemValueList[2], itemValueList[3], itemValueList[4], itemValueList[5]))
# Save (commit) the changes
conn.commit()
所以在我的情况下,在瞬间值5和Value6是“无”或NULL在SQLite的数据库。所以我设置添加到itemValueList为“”的字符串。但是,这并不工作。该DB Entriy犯规被删除。
我有什么改变也参赛作品,其中一些列可以没有任何价值越来越被删除?
谢谢。
[编辑]:
c.execute('delete from Database where isnull(Value1,"")=? and isnull(Value2,"")=? and isnull(Value3,"")=? and isnull(Value4,"")=? and isnull(Value5,"")=? and isnull(Value6,"")=?',
(itemValueList[0], itemValueList[1], itemValueList[2], itemValueList[3], itemValueList[4], itemValueList[5]))
在SQL(特别是,SQLite的),值NULL
不等于空字符串。所以,在itemValueList
的字符串设置为空字符串是没有意义的。你需要改变你的SQL查询来允许NULL
值,如:
delete
from Database
where Value1=?
and Value2=?
and Value3=?
and Value4=?
and Value5 is null
and Value6=?
并使用查询的是形式,如果你是值5 None
,或者您需要将NULL值转换为空字符串:
delete
from Database
where isnull(Value1,'') =?
and isnull(Value2,'') =?
and isnull(Value3,'') =?
and isnull(Value4,'') =?
and isnull(Value5,'') =?
and isnull(Value6,'') =?
见What is the equivalent of the null-safe equality operator <=> in SQLite?。您应该使用IS
运营商,而不是=
在您的查询,并能正确匹配NULL
值。
然后,您需要更改代码使用Python None
而不是一个空字符串。这将准备好的语句翻译成SQL NULL
。
if item.GetText() == "None":
itemValueList.append(None)
else:
itemValueList.append(item.GetText())