May-16-2017, 05:26 PM
Finally figured this one out. Feel kind of stupid that this solution did jump out at me the first time I read the sqlite3 docs.
import sqlite3 import sys table = """CREATE TABLE speakers ( id INTEGER PRIMARY KEY NOT NULL, brand TEXT NOT NULL, type TEXT NOT NULL, size TEXT NOT NULL, price REAL NOT NULL);""" data = [{"brand": "Logitech", "type": "computer", "size": "small", "price": 4.99}, {"brand": "Samsung", "type": "sound bar", "size": "medium", "price": 249.99}, {"brand": "Samsung", "type": "headphones", "size": "small", "price": 24.99}] data2 = [("Logitech", "computer", "small", 4.99), ("Samsung", "sound bar", "medium", 249.99), ("Samsung", "headphones", "small", 24.99)] insert_sql = """INSERT INTO speakers (brand,type,size,price) VALUES (:brand,:type,:size,:price);""" insert_sql2 = """INSERT INTO speakers (brand,type,size,price) VALUES (?,?,?,?);""" select_sql = "SELECT * FROM speakers;" with sqlite3.connect("speakers.db") as conn: cur = conn.cursor() cur.executescript(table) cur.executemany(insert_sql2, data2) conn.commit() for row in cur.execute(select_sql): print(row) sys.exit(0)