关于最新的torndb.py 可以从https://github.com/bdarnell/torndb/blob/master/torndb.py下载。torndb代码结构很是简明清晰,见下图:
图1,torndb代码结构
首先连接到数据库:
import torndb
db=torndb.Connection('localhost','talk',user='root',password='pass')
然后就可以各种操作了,在MySQLdb中所有的操作都是通过execute执行的,而把TA封装之后的torndb,提供了3种,execute,get,query。如下:
cre='create table blog(id int,content text)'
db.execute(cre)
string='wawuee'
exe='insert into blog(id,content)values(%d,"%s")'%(1,string)
db.execute(exe)
execute就是执行不需要返回值的操作,创建表,插入表,删除表等等。
>>> a=db.get('select * from blog where id=1')
>>> a
{'content': 'wawuee', 'id': 1}
>>> a=db.query('select * from blog')
>>> a
[{'id': 250, 'content': 'wahaha'}, {'id': 1, 'content': 'wawuee'}]
get就是取一行数据,返回Row类,其实就是dict;如果要取多行用query,返回是List。
总结下,torndb对MySQLdb封装后,query,get返回是list,dict这些,非常方便,可以直接拿来用,这是TA的优点,而且是默认自动commit的,不用MySQLdb的手动commit,用起来很是简洁。如果有需要的话,我可以写个torndb和MySQLdb的使用对比,都是python3.x下的哈~
转载请注明:转自 http://blog.csdn.net/littlethunder/article/details/8918045
482


&spm=1001.2101.3001.11974&articleId=8918045&d=1&t=3&u=a2ab793b0df34c14978ed001776fd92b)

被折叠的 条评论
为什么被折叠?



