开发者

How do I use prepared statements for inserting MULTIPLE records in SQlite using Python / Django?

开发者 https://www.devze.com 2023-02-24 05:36 出处:网络
How do I use prepared statement for inserting MULTIPLE records in SQlite using Python / 开发者_如何学运维Django?Official Python library documentation: Cursor objects

How do I use prepared statement for inserting MULTIPLE records in SQlite using Python / 开发者_如何学运维Django?


Official Python library documentation: Cursor objects

Python's SQLite libraries don't have prepared statement objects, but they do allow you to use parameterized queries, and to provide more than one set of parameters.

Edit: An example of executemany as requested:

values_to_insert = [(1,"foo"), (2, "bar"), (3, "baz")]

cursor.executemany("""
    INSERT INTO some_table ('item_num', 'item_name')
    VALUES (?, ?)""", values_to_insert)


You can use executemany() and pass an iterator object, e.g. to insert 100 integers and their squares:

def my_iter(x):
    for i in range(x):
        yield i, i*i

cursor.executemany("INSERT INTO my_table VALUES (?, ?)", my_iter(100))
0

精彩评论

暂无评论...
验证码 换一张
取 消