一、torndb数据库简介
在Tornado3.0版本之前提供tornado.database模块用来操做MySQL数据库,而从3.0版本开始,此模块就被独立出来,做为torndb包单独提供。torndb只是对MySQLdb的简单封装,不支持Python 3。mysql
二、torndb安装
pip install torndbweb
三、链接初始化
class Applicatin(tornado.web.application):
def init(self):
handler = [
(r"index/",IndexHandler),
]sql
settings = dic(
template_path = os.path.join(os.path.dirname(file),"templates"),
static_path = os.path.join(os.path.dirname(file),"static")
)数据库
super(Application,self).init(handler,**settings)app
self.db = torndb.Connection(
host = "127,0,0,1",
database = "ihome",
user = "root",
pwd = "mysql"
)ide
四、使用数据库
一、执行语句
一、execute(query,*patameters,**kwparameters)
一、query:要执行的sql语句tornado
二、*patameters:为sql语句中指定的参数进行数据的传值对象
三、**kwparameters:为sql语句中指定的参数进行数据的赋值操做索引
四、返回影响的最后一条自增字段值
二、execute_rowcount(query,(patameters,**kwparameters))
一、query:要执行的sql语句ip
二、*patameters:为sql语句中指定的参数进行数据的传值
三、**kwparameters:为sql语句中指定的参数进行数据的赋值操做
四、返回受影响行数
三、实例:
db.execute("insert into houses(title, position, price, score, comments) values(%s, %s, %s, %s, %s)", "独立装修小别墅", "紧邻文津街", 280, 5, 128)
或
db.execute("insert into houses(title, position, price, score, comments) values(%(title)s, %(position)s, %(price)s, %(score)s, %(comments)s)", title="独立装修小别墅", position="紧邻文津街", price=280, score=5, comments=128)
二、查询语句
一、get(query, parameters, *kwparameters)
一、query:要执行的sql语句
二、*patameters:为sql语句中指定的参数进行数据的传值
三、**kwparameters:为sql语句中指定的参数进行数据的赋值操做
四、返回单行结果或None,若出现多行则报错。返回值为torndb.Row类型,是一个类字典的对象,即同时支持字典的关键字索引和对象的属相访问。
二、query(query, parameters, *kwparameters)
一、query:要执行的sql语句
二、*patameters:为sql语句中指定的参数进行数据的传值
三、**kwparameters:为sql语句中指定的参数进行数据的赋值操做
四、返回多行结果,torndb.Row的列表。