zoukankan      html  css  js  c++  java
  • tornada-数据库

    数据库

    • torndb安装
    • 连接初始化
    • 执行语句
      • execute
      • execute_rowcount
    • 查询语句
      • get
      • query

    与Django框架相比,Tornado没有自带ORM,对于数据库需要自己去适配。我们使用MySQL数据库。

    在Tornado3.0版本以前提供tornado.database模块用来操作MySQL数据库,而从3.0版本开始,此模块就被独立出来,作为torndb包单独提供。torndb只是对MySQLdb的简单封装,不支持Python 3。

    1.torndb安装

    pip install torndb

    2.连接初始化

    我们需要在应用启动时创建一个数据库连接实例,供各个RequestHandler使用。我们可以在构造Application的时候创建一个数据库实例并作为其属性,而RequestHandler可以通过self.application获取其属性,进而操作数据库实例。

    # 放在application.py 最下面
       super(Application, self).__init__(handlers, **settings)
            # 创建一个全局mysql连接实例供handler使用
            self.db = torndb.Connection(
                host="127.0.0.1",
                database="igeek",
                user="root",
                password="mysql"
            )
    
    

    使用数据库

    新建数据库与表:

    # 
    create database igeek default character set utf8; use igeek; create table houses ( id bigint(
    20) unsigned not null auto_increment comment '房屋编号', title varchar(64) not null default '' comment '标题', 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='房屋信息表';



















  • 相关阅读:
    ZCMU训练赛-H(模拟)
    ZCMU训练赛-B(dp/暴力)
    ZCMU训练赛-A(模拟)
    HDU 2045 LELE的RPG难题(递推)
    HDU 2044 一只小蜜蜂(递归)
    HDU 2050 折线分割平面(转)
    对递归的理解归纳(转)
    漫谈递归思想(转)
    2017中南大学暑期集训day1 : debug&STL-A
    探寻宝藏--河南省第六届大学生程序设计竞赛
  • 原文地址:https://www.cnblogs.com/Mint-diary/p/9919379.html
Copyright © 2011-2022 走看看