-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathexpressions-2-insert.py
More file actions
36 lines (27 loc) · 938 Bytes
/
expressions-2-insert.py
File metadata and controls
36 lines (27 loc) · 938 Bytes
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
from sqlalchemy import MetaData
from sqlalchemy import Table, Column
from sqlalchemy import Integer, String
metadata = MetaData() # metadata is collection of tables and can be traversed like XML DOM
user_table = Table("user", metadata,
Column("id", Integer, primary_key=True),
Column("name", String)
)
# init engine over database
from sqlalchemy import create_engine
engine = create_engine("sqlite://", echo=True)
# create all tables from metadata
metadata.create_all(engine)
# insert data
sql = user_table.insert().values(name = "slavo")
conn = engine.connect();
result = conn.execute(sql);
conn.close();
# insert data in batch
conn = engine.connect();
result = conn.execute(user_table.insert(), [{"name":"jano"}, {"name":"stano"}])
conn.close();
# insert data in batch variant
sql = user_table.insert().values([{"name":"peter"}, {"name":"filip"}])
conn = engine.connect();
result = conn.execute(sql);
conn.close();