zoukankan      html  css  js  c++  java
  • python中封装pymysql库连接mysql数据库

    
    
    import pymysql

    class MysqlHandler:
    #建立数据库连接,新建一个查询页面
    def __init__(self):
    # port要转为int
    self.mysql = pymysql.connect(host=handler.get_option_value("mysql", "connection"),
    user=handler.get_option_value("mysql", "user"),
    password=handler.get_option_value("mysql", "password"),
    port= int(handler.get_option_value("mysql", "port")), charset="utf8", autocommit=True)
    self.cursor = self.mysql.cursor(pymysql.cursors.DictCursor) # fetchone 是字典 fetchalllistdict

    #获取查询结果,结果是dict,上面建立游标指定了是dict
    def query_one(self,sql):
    self.cursor.execute(sql)
    data = self.cursor.fetchone()
    return data

    #关闭查询页面,关闭连接
    def close(self):
    self.cursor.close()
    self.mysql.close()


  • 相关阅读:
    线段树
    2016.9.4
    使用CSS代码修改博客模板
    爬虫
    PHP初学[DAY2]
    2016.8.23
    一个自动设置游戏房间的脚本
    可逆矩阵生成
    #2284. 接水果(fruit)
    #3762. 有趣的数(number)
  • 原文地址:https://www.cnblogs.com/xintiaoab/p/13433775.html
Copyright © 2011-2022 走看看