使用SQLAlchemy

使用SQLAlchemy

参考:
http://www.sqlalchemy.org/
https://www.keakon.net/2012/12/03/SQLAlchemy使用经验 详解
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0014021031294178f993c85204e4d1b81ab032070641ce5000
http://blog.csdn.net/dupei/article/details/6014488
http://www.cnblogs.com/coder2012/archive/2015/08/21/4746941.html join详解
http://gashero.yeax.com/?p=6#id17 SQLAlchemy指南(tutorial)

有人说MySQL-Python 来连 MySQL,因为不支持异步调用,所以和 Tornado 不是很搭,不踩这个坑。

SQLAlchemy 自己维护了一个数据库连接池(默认 5 个连接),因此初始化一个会话的开销并不大
对其他 Web 服务器来说,可以使用 sqlalchemy.orm.scoped_session,它能保证每个线程获得的 session 对象都是唯一的。不过 Tornado 本身就是单线程的,如果使用了异步方式,就可能会出现问题,因此我并没使用它。

拿到 session 后,就可以执行 SQL 了:
复制代码 代码如下:

session.execute('create database abc')
print session.execute('show databases').fetchall()
session.execute('use abc')
# 建 user 表的过程略
print session.execute('select * from user where id = 1').first()
print session.execute('select * from user where id = :id', {'id': 1}).first()

posted @ 2016-10-12 07:51  CooMark  阅读(389)  评论(0编辑  收藏  举报