python中连接mysql

Mysql5添加了对游标的支持,游标主要用于交互式应用,其中用户需要滚动屏幕上的数据,并对数据进行浏览或做出更改,游标只能用于存储过程(和函数)。

存储过程就是为以后使用而保存的一条或多条Mysql语句的集合,使用存储过程具有简单、安全、高性能的好处

python中连接数据库

根据业务需求对pymysql库做二次封装

import pymysql

class HandleMysql:
    def __init__(self):
        self.conn = pymysql.connect(host='',
                                    user='',
                                    password='',
                                    port='',
                                    database='',
                                    charset="utf8",
                                    cursorclass=pymysql.cursors.DictCursor)
        self.cursor = self.conn.cursor()

    def get_one_value(self, sql, args=None):
        self.cursor.execute(sql, args=args)
        self.conn.commit()
        return self.cursor.fetchone()

    def get_values(self, sql, args=None):
        self.cursor.execute(sql, args=args)
        self.conn.commit()
        return self.cursor.fetchall()

    def close(self):
        self.cursor.close()
        self.conn.close()
原文地址:https://www.cnblogs.com/donghe123/p/13661682.html