将列表列表中的1列值插入表

时间:2016-02-24 00:45:32

标签: python sqlite

我有一份清单清单 log = [["MD103", None, None, None, None, None], ["MD102", None, None, None, None, None]]

我想在

上插入每个列表的第一个值

cur.executemany("INSERT INTO devices VALUES(?)", logValues[1],)

我知道这是错的,但我无法知道如何做到这一点。有时,当它工作时,它会通过单独的方式将每个字符插入表中。

这里是整个代码:

import sqlite3 as lite
import sys


con = None
log = [["MD103", None, None, None, None, None], ["MD102", None, None, None, None, None]]

def logMovements(logValues):
try:
    con = lite.connect('netgate.db')    
    cur = con.cursor()
    try:
        cur.executemany("INSERT INTO devices VALUES(?)", logValues[1],)
    except lite.IntegrityError:
        print "Device already on database"
    cur.commit()
    con.executemany("INSERT INTO firmwares VALUES(?,?,?,?,?,?,'False','None')", (logValues))
    con.commit()                   
    cur.execute("SELECT * FROM devices")
    print cur.fetchall()
    cur.execute("SELECT * FROM firmwares")
    print cur.fetchall()

finally:
    if con:
        con.close()

logMovements(log)

编辑:此代码抛出此错误。 提供的绑定数量不正确。当前语句使用1,并且提供了5个

编辑2:问题在于第一次插入,因为第二次正常工作:P

1 个答案:

答案 0 :(得分:1)

从所有条目中提取第一个值:

devices = [entry[0] for entry in log]
cur.executemany("INSERT OR IGNORE INTO devices VALUES(?)", devices)