|
from sqlalchemy import ( |
|
create_engine, |
|
MetaData, |
|
Table, |
|
Column, |
|
String, |
|
Integer, |
|
Float, |
|
insert, |
|
) |
|
|
|
|
|
engine = create_engine("sqlite:///:memory:") |
|
metadata_obj = MetaData() |
|
|
|
|
|
receipts = Table( |
|
"receipts", |
|
metadata_obj, |
|
Column("receipt_id", Integer, primary_key=True), |
|
Column("customer_name", String(16), primary_key=True), |
|
Column("price", Float), |
|
Column("tip", Float), |
|
) |
|
|
|
|
|
metadata_obj.create_all(engine) |
|
|
|
|
|
def insert_rows_into_table(rows, table): |
|
with engine.begin() as connection: |
|
connection.execute(insert(table), rows) |
|
|
|
|
|
rows = [ |
|
{"receipt_id": 1, "customer_name": "Alan Payne", "price": 12.06, "tip": 1.20}, |
|
{"receipt_id": 2, "customer_name": "Alex Mason", "price": 23.86, "tip": 0.24}, |
|
{"receipt_id": 3, "customer_name": "Woodrow Wilson", "price": 53.43, "tip": 5.43}, |
|
{"receipt_id": 4, "customer_name": "Margaret James", "price": 21.11, "tip": 1.00}, |
|
] |
|
|
|
|
|
insert_rows_into_table(rows, receipts) |
|
|