Debian Codegraph @

examples/example_simple_sa_oldstyle.py

raw
1 import asyncio
2 import sqlalchemy as sa
3
4 from aiomysql.sa import create_engine
5
6
7 metadata = sa.MetaData()
8
9 tbl = sa.Table('tbl', metadata,
10 sa.Column('id', sa.Integer, primary_key=True),
11 sa.Column('val', sa.String(255)))
12
13
14 @asyncio.coroutine
15 def create_table(engine):
16 with (yield from engine) as conn:
17 yield from conn.execute('DROP TABLE IF EXISTS tbl')
18 yield from conn.execute('''CREATE TABLE tbl (
19 id serial PRIMARY KEY,
20 val varchar(255))''')
21
22
23 @asyncio.coroutine
24 def go():
25 engine = yield from create_engine(user='root',
26 db='test_pymysql',
27 host='127.0.0.1',
28 password='')
29
30 yield from create_table(engine)
31 with (yield from engine) as conn:
32 yield from conn.execute(tbl.insert().values(val='abc'))
33
34 res = yield from conn.execute(tbl.select())
35 for row in res:
36 print(row.id, row.val)
37
38 yield from conn.commit()
39
40 engine.close()
41 yield from engine.wait_closed()
42
43
44 asyncio.get_event_loop().run_until_complete(go())