用tornado3.0.1所以开始使用torndb,对torndb进行学习。
链接到数据库查看torndb提供的方法如下:
close、database、execute、execute_lastrowid、execute_rowcount、
executemany、executemany_lastrowid、executemany_rowcount、get、
host、iter、max_idle_time、query、reconnect、socket
提供的方法
execute 执行语句不需要返回值的操作。
execute_lastrowid 执行后获得表id,一般用于插入后获取返回值。
executemany 可以执行批量插入。返回值为第一次请求的表id。
executemany_rowcount 批量执行。返回值为第一次请求的表id。
get 执行后获取一行数据,返回dict。
iter 执行查询后,返回迭代的字段和数据。
query 执行后获取多行数据,返回是List。
close 关闭
max_idle_time 最大连接时间
reconnect 关闭后再连接
torndb可以自动commit不用手动提交,对几个主要的方法进行各测试。
建表
CREATE TABLE `ceshi` (
`id` int(1) NULL AUTO_INCREMENT ,
`num` int(1) NULL ,
PRIMARY KEY (`id`)
)
;
>>> import torndb
>>> db = torndb.Connection(“192.168.1.103″,”ceshi”,”ceshi”, “1111111”, 24*3600)
>>> get_id1 = db.execute_lastrowid(“insert ceshi(num) values(‘1’)”)
>>> print get_id1
1
>>> args1 = [(‘2’),(‘3’),(‘4’)]
>>> get1 = db.executemany(“insert ceshi(num) values(%s)”, args1)
>>> print get1
2
>>> args2 = [(‘5’),(‘6’),(‘6’)]
>>> get2 = db.executemany(“insert ceshi(num) values(%s)”, args2)
>>> print get2
5
>>> rows = db.iter(“select * from ceshi”)
>>> for i in rows:
… print i
…
{‘num’: 1L, ‘id’: 1L}
{‘num’: 2L, ‘id’: 2L}
{‘num’: 3L, ‘id’: 3L}
{‘num’: 4L, ‘id’: 4L}
{‘num’: 5L, ‘id’: 5L}
{‘num’: 6L, ‘id’: 6L}
{‘num’: 6L, ‘id’: 7L}
发表评论