jcubic
jcubic

Reputation: 66518

Python's executemany for sqlite3 don't accept list of tuples

I'm trying to create sqlite database from git commits, I have code like this:

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()

the log function return git commits as list of dictionaries, and I've got exception:

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

I'm using Python 2.7.13

Upvotes: 1

Views: 640

Answers (1)

PRMoureu
PRMoureu

Reputation: 13327

you need to specify how many values you want to insert :

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

Upvotes: 1

Related Questions