from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
engine = create_engine('mysql://用户:密码@localhost/库?charset=utf8',encoding='utf-8',echo=True,pool_recycle=60)
# 表blog
class blog(Base):
__tablename__ = 'blog'
id = Column('id', Integer, primary_key = True)
content = Column('content', String(512), nullable = False)
gmtCreate = Column('gmtCreate', TIMESTAMP, server_default=text('NOW()'))
def __init__(self, content):
self.uid = uid
self.content = content
# 创建表
metadata = Base.metadata
metadata.create_all(engine)
#orm 操作
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import scoped_session
sessionFac = sessionmaker(bind=engine, autocommit=True, autoflush=True)
orm = scoped_session(sessionFac)
## 增加
b = blog('fffffffffffffff')
orm.add(b)
orm.flush()
## 查询
r = orm.query(blog)
for e in r:
print e.id
e = orm.query(blog).filter_by(id=1)
print e.id