SQLAlchemy (三)---使用操作连接符

建立Python文件alchemy10.py,代码如下:

#!/usr/bin/ python
#encoding:utf8

from sqlalchemy import *
from sqlalchemy.orm import *
#alchemy5实现创建数据库表格功能
from alchemy5 import *

#功能:测试操作连接
#这里的连接指条件查询的时候,逻辑运算符的连接,即 and or 和 not
print '#'*15
union = and_(user.c.name.like('%j'),
             user.c.id == address.c.user_id,
        or_(address.c.email == '[email protected]',
            address.c.email == '[email protected]'),
        not_(user.c.id>5)
        )
print union
print '#'*15
#######################完整例子#########################3
se_sql = [(user.c.fullname +", " + address.c.email).label('title')]
wh_sql = and_(
              user.c.id == address.c.user_id,
              user.c.name.between('m', 'z'),
              or_(
                  address.c.email.like('%@aol.com'),
                  address.c.email.like('%@msn.com')
              )
         )
#print wh_sql
s = select(se_sql).where(wh_sql) #第一步 select()
#print s
r = conn.execute(s) #第二步 execute()
result = r.fetchall()   #第三步 fetchall()取结果
print result
print '#'*15

SQLAlchemy (三)---使用操作连接符_第1张图片

你可能感兴趣的:(数据库)