forked from dongweiming/web_develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_sql.py
More file actions
44 lines (32 loc) · 1.13 KB
/
text_sql.py
File metadata and controls
44 lines (32 loc) · 1.13 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
# coding=utf-8
from sqlalchemy import create_engine, Column, Integer, String, Sequence, text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from consts import DB_URI
eng = create_engine(DB_URI)
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, Sequence('user_id_seq'),
primary_key=True, autoincrement=True)
name = Column(String(50))
Base.metadata.drop_all(bind=eng)
Base.metadata.create_all(bind=eng)
Session = sessionmaker(bind=eng)
session = Session()
session.add_all([User(name=username)
for username in ('xiaoming', 'wanglang', 'lilei')])
session.commit()
def get_result(rs):
print '-' * 20
for user in rs:
print user.name
rs = session.query(User).filter(
text('id > 2 and id < 4')).order_by(text('id')).all()
get_result(rs)
rs = session.query(User).filter(text('id<:value and name=:name')).params(
value=3, name='xiaoming').all()
get_result(rs)
rs = session.query(User).from_statement(
text('SELECT * FROM users where name=:name')).params(name='wanglang').all()
get_result(rs)