Python的smanite3的executemany不接受元组列表

时间:2017-10-22 09:09:13

标签: python sqlite

我正在尝试从git提交创建sqlite数据库,我有这样的代码:

if __name__ == '__main__':
    path = os.getcwd()
    commits = log(path)
    if path.endswith("/"):
        path = path + "/"
    fname = "%sgit.sqlite" % (path)
    if os.path.isfile(fname):
        os.remove(fname)
    con = sqlite3.connect(fname)
    con.execute("CREATE TABLE commits (hash VARCHAR(40), author VARCHAR(256), email VARCHAR(256), " +
                "message text, date VARCHAR(35))")
    cur = con.cursor()
    def commit(c):
        return (c["hash"], c["author"], c["email"], c["message"], c["date"])
    cur.executemany("INSERT INTO commits VALUES(?)", map(commit, commits))
    # this prints 2d array
    #print json.dumps(map(commit, commits))
    # this print tuple
    print type(map(commit, commits)[0])
    # this print 5
    print len(map(commit, commits)[0])

    con.close()

日志函数返回git提交为字典列表,我有异常:

Traceback (most recent call last):
  File "/home/kuba/bin/gitsql.py", line 48, in <module>
    cur.executemany("INSERT INTO commits VALUES(?)", map(commit, commits))
sqlite3.OperationalError: table commits has 5 columns but 1 values were supplied

我正在使用Python 2.7.13

1 个答案:

答案 0 :(得分:1)

您需要指定要插入的值的数量:

cur.executemany("INSERT INTO commits VALUES(?, ?, ?, ?, ?)", map(commit, commits))