| import sqlalchemy
|
| from sqlalchemy import create_engine
|
| from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
|
| from sqlalchemy import inspect
|
| from sqlalchemy.sql import text
|
|
|
| metadata = MetaData()
|
| books = Table('book', metadata,
|
| Column('id', Integer, primary_key=True),
|
| Column('title', String),
|
| Column('primary_author', String),
|
| )
|
|
|
| engine = create_engine('sqlite:///bookstore.db')
|
| metadata.create_all(engine)
|
|
|
|
|
| with engine.connect() as con:
|
|
|
| data = ( { 'id': 1, 'title': 'The Hobbit', 'primary_author': 'Tolkien' },
|
| { 'id': 2, 'title': 'The Silmarillion', 'primary_author': 'Tolkien' },
|
| )
|
| print(str(data))
|
| statement = text('''INSERT INTO book(id, title, primary_author) VALUES(:id, :title, :primary_author)''')
|
|
|
| for line in data:
|
| con.execute(statement, **line)
|
|
|
|
|
| with engine.connect() as con:
|
|
|
| rs = con.execute('SELECT * FROM book')
|
|
|
| for row in rs:
|
| print(row)
|