gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:36:51 25 4
gpt4 key购买 nike

我正在尝试从 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 commits 作为字典列表,但我遇到了异常:

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

最佳答案

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

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

关于Python的sqlite3的executemany不接受元组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46872357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com