zoukankan      html  css  js  c++  java
  • 连接mysql数据库

    封装pymysql, 代码展示:

    # encoding:utf-8
    import pymysql.cursors
    
    
    
    
    class MysqlOperation(object):
        def __init__(self, config):
            self.connection = pymysql.connect(host=config['mysql_host'],
                                              port=config['mysql_port'],
                                              user=config['mysql_user'],
                                              # pymysql直接连接是passwd,用连接池连接是password
                                              passwd=config['mysql_passwd'],
                                              db=config['mysql_db'],
                                              charset='utf8',
                                              cursorclass=pymysql.cursors.DictCursor
                                              )
        
        def read_sql(self, sql):
            with self.connection.cursor() as cursor:
                try:
                    cursor.execute(sql)
                    result = cursor.fetchall()
                    return result
                except Exception as e:
                    self.connection.rollback()  # 回滚
                    print('事务失败', e)
        
        def insert_sql(self, sql):
            with self.connection.cursor() as cursor:
                try:
                    cursor.execute(sql)
                    self.connection.commit()
                except Exception as e:
                    self.connection.rollback()
                    print('事务失败', e)
        
        def update_sql(self, sql):
            # sql_update ="update user set username = '%s' where id = %d"
            
            with self.connection.cursor() as cursor:
                try:
                    cursor.execute(sql)  # 像sql语句传递参数
                    # 提交
                    self.connection.commit()
                except Exception as e:
                    # 错误回滚
                    self.connection.rollback()
        
        def delect_sql(self, sql_delete):
            with self.connection.cursor() as cursor:
                try:
                    cursor.execute(sql_delete)  # 像sql语句传递参数
                    # 提交
                    self.connection.commit()
                except Exception as e:
                    # 错误回滚
                    self.connection.rollback()
        
        def read_one(self, sql):
            with self.connection.cursor() as cursor:
                try:
                    cursor.execute(sql)
                    result = cursor.fetchone()
                    return result
                except Exception as e:
                    self.connection.rollback()  # 回滚
                    print('事务失败', e)
        
        def reConnect(self):
            try:
                self.connection.ping()
            except:
                self.connection()
    
        def callpro_sql(self, proc_name, *args):
            with self.connection.cursor(self.connection.cursorclass) as cursor:
                try:
                    cursor.callproc(proc_name, args)  # 传递存储过程名和参数
                    self.connection.commit()           # 提交
                    print('调用存储过程结束')
                except Exception as e:
                    self.connection.rollback()  # 回滚
                    print('事务失败', e)
  • 相关阅读:
    女人的话中话(英文版),供男生参考哦
    那个时候的我(漫画连载)
    杨过与小龙女
    盛大正式收购SINA19.5%股份
    揭开SVCHOST.exe进程之谜
    该呼呼咯,各位朋友晚安~
    图解学说上海话
    2005年的12个祝福(2005年传统精美挂历)
    iframe 的自适应高度
    【蜡笔小新全集】+动漫【灌蓝高手】 高速在线看
  • 原文地址:https://www.cnblogs.com/qianslup/p/13176072.html
Copyright © 2011-2022 走看看