我的表中的列如下所示:
姓名1 |姓名2 |体重|身高|数字1 |数字2
有些Number1和Number2列是空的,我只想更新空行。为此,我有6个列表:
List1 contains the values in Name1, in the same order
List2 contains the values in Name2, in the same order
List3 contains the values in Name1, but in a different order
List4 contains the values in Name2, but in the same order as List3
列表5和6分别包含要插入数字1和数字2的值,其顺序与列表3和4相同
所以我想做的是:
在表中搜索Number1和Number2中具有空值的所有行
在列表3或4中搜索与Name1或Name2匹配的值,因此如果在列表3中找到匹配项,则Number1和Number2的值仍应分别从列表5和6中填充
所以我想到的代码是这样的:
for i in Name1:
c.execute("SELECT * FROM Mytable WHERE Number1 IS NULL")
if Name1 is List3:
c.execute("INSERT INTO Mytable (Number1, Number2)" " VALUES (?, ?)",
(List5[i], List6[i]))
elif Name2 is List4:
c.execute("INSERT INTO Mytable (Number1, Number2)" " VALUES (?, ?)",
(List5[i], List6[i]))
但是,当我运行它时,它会向表中添加行,但不会为Number1或Number2添加值。我哪里出错了
可能在这里:
is
关键字测试object identity,这不是您想要的。即使是平等的测试在这里也帮不了你,因为两个列表中的顺序是不同的结果是没有执行任何
if
分支。为了实现您的目标,您需要测试两个列表是否包含相同的成员:Testing if a list contains another list with Python
但是,您的问题不清楚确切的条件:Name1/List3和Name2/List4是否应该包含完全相同的成员,或者一个成员包含在另一个成员中就足够了?你也许能自己想出解决办法;如果不是,我建议你就这个具体问题再问一个问题
相关问题 更多 >
编程相关推荐