python sqlite insert named parameters or null

后端 未结 2 864
谎友^
谎友^ 2021-02-19 01:54

I\'m trying to insert data from a dictionary into a database using named parameters. I have this working with a simple SQL statement e.g.

SQL = \"INSERT INTO sta         


        
2条回答
  •  梦谈多话
    2021-02-19 02:20

    There is a simpler solution to this problem that should be feasible in most cases; just pass to executemany a list of defaultdict instead of a list of dict.

    In other words, if you build from scratch your rows as defaultdict you can pass the list of defaultdict rows directly to the command executemany, instead of building them as dictionaries and later patch the situation before using executemany.

    The following working example (Python 3.4.3) shows the point:

    import sqlite3
    from collections import defaultdict
    # initialization
    db = sqlite3.connect(':memory:')
    c = db.cursor()
    c.execute("CREATE TABLE status(location TEXT, arrival TEXT, departure TEXT)")
    SQL = "INSERT INTO status VALUES (:location, :arrival, :departure)"
    # build each row as a defaultdict
    f = lambda:None # use str if you prefer
    row1 = defaultdict(f,{'location':'place1', 'departure':'1000'})
    row2 = defaultdict(f,{'location':'place2', 'arrival':'1010'})
    rows = (row1, row2)
    # insert rows, executemany can be safely used without additional code
    c.executemany(SQL, rows)
    db.commit()
    # print result
    c.execute("SELECT * FROM status")
    print(list(zip(*c.description))[0])
    for r in c.fetchall():
        print(r)
    db.close()
    

    If you run it, it prints:

    ('location', 'arrival', 'departure')
    ('place1', None, '1000') # None in Python maps to NULL in sqlite3
    ('place2', '1010', None)
    

提交回复
热议问题