2017-02-23 35 views
0

我的表中的列看起来像这样:更新列表中的sqlite3 db

Name1 | Name2 |重量|高度| Number1 | Number2

一些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

那么什么我试图做的是:

  1. 在表1中搜索Number1和Number2中具有空值的所有行;

  2. 搜索列出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])) 

然而,当我运行它,它增加了行的表,但没有值数字1或数字2。我哪里错了?

回答

0

我要去哪里错了?

大概位置:

if Name1 is List3: 
[...] 
elif Name2 is List4: 

is关键字试验object identity,这是不是你想要的这里。即使是平等的测试也无济于事,因为这两份名单的顺序都不相同。

>>> a = ["a", "b", "c"] 
>>> b = a 
>>> a is b 
True 
>>> a = ["a", "b", "c"] 
>>> b = ["a", "b", "c"] 
>>> a is b 
False 
>>> a == b 
True 
>>> b = ["b", "c", "a"] 
>>> a == b 
False 

由此产生的行为是,您的if分支都没有执行。为了达到你的目标,你需要测试,如果两个列表包含相同的成员:

Testing if a list contains another list with Python

然而,你的问题是不清楚确切的条件:有名称1 /项目list3和NAME/List4应该包含准确同一个成员,还是一个成员被包含在另一个成员中就足够了?您可能能够自己找出解决方案;如果没有,我建议你就这个具体问题开一个问题。