sql database.sql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql database.sql相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import pymysql

from tormysql import ConnectionPool
from tornado import gen

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

from tornado.options import define, options

define("port", default=8888, help="run on the given port", type=int)

pool = ConnectionPool(
    max_connections=int(os.getenv("MYSQL_POOL", 5)),
    idle_seconds=7200,
    user="test",
    passwd="test",
    db="test",
    charset="utf8",
    no_delay=True,
    sql_mode="REAL_AS_FLOAT",
    init_command="SET max_join_size=DEFAULT"
)

@gen.coroutine
def get_data(pool):
    sql = """select id, value from mytable;"""
    with (yield pool.Connection()) as conn:
        try:
            with conn.cursor(pymysql.cursors.DictCursor) as cursor:
                yield cursor.execute(sql)
                datas = cursor.fetchall()
                print(datas)
                return datas
        except Exception as e:
            yield conn.rollback()
            raise e
        else:
            yield conn.commit()

class MainHandler(tornado.web.RequestHandler):
    @gen.coroutine
    def get(self):
        datas = yield get_data(pool)
        print("datas is ", datas)
        self.write("YES")


def main():
    tornado.options.parse_command_line()
    application = tornado.web.Application([
        (r"/", MainHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()


if __name__ == "__main__":
    main()
mysql> show create table mytable\G
*************************** 1. row ***************************
       Table: mytable
Create Table: CREATE TABLE `mytable` (
  `id` int(11) DEFAULT NULL,
  `value` varchar(32) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

以上是关于sql database.sql的主要内容,如果未能解决你的问题,请参考以下文章

使用“database/sql”时如何防止 Go 中的 SQL 注入攻击?

Golang database/sql源码分析

sql utf8_database.sql

Oracle Database-PL/SQL

go database/sql sql-driver/mysql 操作

database/sql接口使用