Python 从列表更新sqlite3数据库

Python 从列表更新sqlite3数据库,python,list,sqlite,Python,List,Sqlite,“我的表”中的列如下所示: 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.ex

“我的表”中的列如下所示:

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 |重量|高度|编号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分别包含要插入到Number1和Number2中的值,其顺序与列表3和4相同

所以我想做的是:

  • 在表中搜索Number1和Number2中具有空值的所有行

  • 在列表3或4中搜索与Name1或Name2匹配的值,因此如果在列表3中找到匹配项,则应分别从列表5和6中填写Number1和Number2的值

  • 我想到的代码是这样的:

    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添加任何值。我哪里做错了

    我哪里做错了

    可能在这里:

    if Name1 is List3:
    [...]
    elif Name2 is List4:
    
    关键字测试,这不是您想要的。在这里,即使是平等测试也帮不了你,因为两个列表中的顺序是不同的

    >>> 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
    分支。为了实现目标,您需要测试两个列表是否包含相同的成员:

    但是,您的问题不清楚确切的条件:Name1/List3和Name2/List4应该包含完全相同的成员,还是一个成员包含在另一个成员中就足够了?你也许能自己想出解决办法;如果不是,我建议你就这个具体问题再问一个问题