Python SQLite delete,其中值为;无”;
我需要删除值可能为None的DB条目。我从列表框中获取我的值。 函数如下所示:Python SQLite delete,其中值为;无”;,python,sqlite,Python,Sqlite,我需要删除值可能为None的DB条目。我从列表框中获取我的值。 函数如下所示: 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 = s
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)
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()
我的DBManager中的函数如下所示:
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)
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()
因此,在我的例子中,Value5和Value6在SQLite数据库中是“None”或NULL。因此,我将添加到itemValueList的字符串设置为“”。但这不起作用。数据库entry不会被删除
如果删除了某些列可能没有值的条目,我需要做什么更改
多谢各位
[编辑]:
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=?
如果Value5是None
,或者需要将空值转换为空字符串,则使用该查询形式:
delete
from Database
where isnull(Value1,'') =?
and isnull(Value2,'') =?
and isnull(Value3,'') =?
and isnull(Value4,'') =?
and isnull(Value5,'') =?
and isnull(Value6,'') =?
在SQL(尤其是SQLite)中,值NULL
与空字符串不同。因此,将itemValueList
中的字符串设置为空字符串毫无意义。您需要更改SQL查询以允许NULL
值,如:
delete
from Database
where Value1=?
and Value2=?
and Value3=?
and Value4=?
and Value5 is null
and Value6=?
如果Value5是None
,或者需要将空值转换为空字符串,则使用该查询形式:
delete
from Database
where isnull(Value1,'') =?
and isnull(Value2,'') =?
and isnull(Value3,'') =?
and isnull(Value4,'') =?
and isnull(Value5,'') =?
and isnull(Value6,'') =?
看。您应该在查询中使用IS
运算符,而不是=
,它将正确匹配NULL
值
然后需要将代码更改为使用PythonNone
而不是空字符串。这将在准备好的语句中转换为SQLNULL
if item.GetText() == "None":
itemValueList.append(None)
else:
itemValueList.append(item.GetText())
看。您应该在查询中使用IS
运算符,而不是=
,它将正确匹配NULL
值
然后需要将代码更改为使用PythonNone
而不是空字符串。这将在准备好的语句中转换为SQLNULL
if item.GetText() == "None":
itemValueList.append(None)
else:
itemValueList.append(item.GetText())
空字符串与
NULL
不同。要匹配SQL中的空值,必须使用columnName IS null
,不能使用=
。空字符串与null
不同。要匹配SQL中的空值,必须使用columnName IS null
,不能使用=
。如果每个值都可以为空,如何使用该值。我不知道什么值是空的。@K-Doe对每个值都使用与我显示的Value5
相同的短语吗?!我会更新我的答案我试图用你的代码转换我的代码。但现在我得到:OperationalError:near“isnull”:语法错误。我将添加到我的帖子中。@K-Doe您使用的是双引号。SQL不知道双引号。使用单引号,就像我在回答SQL中所做的那样。SQL不是Python,不能交换双引号和单引号。明白了!谢谢。如果每个值都可以为空,我该如何使用它呢。我不知道什么值是空的。@K-Doe对每个值都使用与我显示的Value5
相同的短语吗?!我会更新我的答案我试图用你的代码转换我的代码。但现在我得到:OperationalError:near“isnull”:语法错误。我将添加到我的帖子中。@K-Doe您使用的是双引号。SQL不知道双引号。使用单引号,就像我在回答SQL中所做的那样。SQL不是Python,不能交换双引号和单引号。明白了!非常感谢。