tornada-数据库
生活随笔
收集整理的這篇文章主要介紹了
tornada-数据库
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
數(shù)據(jù)庫
- torndb安裝
- 連接初始化
- 執(zhí)行語句
- execute
- execute_rowcount
- 查詢語句
- get
- query
與Django框架相比,Tornado沒有自帶ORM,對(duì)于數(shù)據(jù)庫需要自己去適配。我們使用MySQL數(shù)據(jù)庫。
在Tornado3.0版本以前提供tornado.database模塊用來操作MySQL數(shù)據(jù)庫,而從3.0版本開始,此模塊就被獨(dú)立出來,作為torndb包單獨(dú)提供。torndb只是對(duì)MySQLdb的簡單封裝,不支持Python 3。
1.torndb安裝
pip install torndb2.連接初始化
我們需要在應(yīng)用啟動(dòng)時(shí)創(chuàng)建一個(gè)數(shù)據(jù)庫連接實(shí)例,供各個(gè)RequestHandler使用。我們可以在構(gòu)造Application的時(shí)候創(chuàng)建一個(gè)數(shù)據(jù)庫實(shí)例并作為其屬性,而RequestHandler可以通過self.application獲取其屬性,進(jìn)而操作數(shù)據(jù)庫實(shí)例。
# 放在application.py 最下面super(Application, self).__init__(handlers, **settings)# 創(chuàng)建一個(gè)全局mysql連接實(shí)例供handler使用self.db = torndb.Connection(host="127.0.0.1",database="igeek",user="root",password="mysql")?
使用數(shù)據(jù)庫
新建數(shù)據(jù)庫與表:
#create database igeek default character set utf8; use igeek; create table houses (id bigint(20) unsigned not null auto_increment comment '房屋編號(hào)',title varchar(64) not null default '' comment '標(biāo)題',position varchar(32) not null default '' comment '位置',price int not null default 0,score int not null default 5,comments int not null default 0,primary key(id) )ENGINE=InnoDB default charset=utf8 comment='房屋信息表';
?
轉(zhuǎn)載于:https://www.cnblogs.com/Mint-diary/p/9919379.html
總結(jié)
以上是生活随笔為你收集整理的tornada-数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 输出控制台信息到日志 并 通过crono
- 下一篇: MYSQL重置ROOT密码