tornado连接数据库:
current_path =os.path.dirname(__file__)
app = tornado.web.Application(
[(r"/", IndexHandler),
(r"/(.*)",StaticFileHandler, {"path":os.path.join(current_path,"statics/html"), default_filename:"index.html"}), --这个要配置到最后一个
],
static_path = os.path.join(current_path ,"statics"),
template_path = os.path.join(current_path ,"template"),
debug=True)
app.db= tornado.Connect(
host="127.0.0.1",
database ="xf",
user ='root',
password="mysql"
)
对数据库进行操作:
calss IndexHandler(RequestHandler):
def get(self):
ret = self.applicantion.db.get('select ui_name from ui_user_info where ui_uiser_id=1')
self.write(ret['ui_name'])
1、执行语句 ---增删改操作
execute(query, parameters, *kwparameters)返回影响的最后一条自增字段值
execute_rowcount(query, parameters, *kwparameters) 返回影响的行数
query--执行的sql语句
parameters, *kwparameters -- 绑定的参数
2、查询语句
get(query, parameters, *kwparameters) 返回单行结果或者None,如出现多行则报错。返回值是table.Row类型,是一个类字典对象。
query(query, parameters, *kwparameters) 返回多行结果,table.Row列表