-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLAlchemy-I
More file actions
50 lines (33 loc) · 1.58 KB
/
SQLAlchemy-I
File metadata and controls
50 lines (33 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from sqlalchemy import select, Table, Column, Integer, String, MetaData, ForeignKey, create_engine
from config import subd, driver, user_name, password, host, port, db_name
meta = MetaData()
authors = Table('Authors', meta,
Column('id_author', Integer, primary_key=True),
Column('name', String(250), nullable=False))
books = Table('Books', meta,
Column('id_book', Integer, primary_key=True),
Column('title', String(250), nullable=False),
Column('author_id', Integer, ForeignKey('Authors.id_author')),
Column('genre', String(250)),
Column('price', Integer))
print(books.c.author_id)
print(authors.c.name)
try:
engine = create_engine(subd + "+" + driver + "://" + user_name + ":" + password + "@" + host + ":" + port + "/" + db_name)
books.create(engine)
authors.create(engine)
conn = engine.connect()
ins_author_query = authors.insert().values(name='Lutz')
conn.execute(ins_author_query)
ins_book_query = books.insert().values(title='Learn Python', author_id=1, genre='Education', price=1299)
conn.execute(ins_book_query)
ins_book_query2 = books.insert().values(title='Clear Python', author_id=1, genre='Education', price=956)
conn.execute(ins_book_query2)
books_gr_1000_query = books.select().where(books.c.price > 1000) # SELECT * FROM Books WHERE Books.price > 1000;
result = conn.execute(books_gr_1000_query)
for row in result:
print(row)
print("Success")
except Exception as ex:
print("Connection refused")
print(ex)