zoukankan      html  css  js  c++  java
  • Python 操控Mysql

    import pymysql
    
    class MyDBConnector:
    
        def __init__(self, logger, host, user, password, schema, charset):
            self.logger = logger
            self.host = host
            self.user = user
            self.password = password
            self.schema = schema
            self.charset = charset
            self.logger.info(('init with:{}|{}|{}|{}'.format(host, user, password, schema, charset)))
    
        def __connect(self):
            self.conn = pymysql.connect(host=self.host,
                                        user=self.user,
                                        password=self.password,
                                        database=self.schema,
                                        charset=self.charset)
            self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
            self.logger.info('connected')
    
        def __disconnect(self):
            self.cursor.close()
            self.conn.close()
            self.logger.info('disconnected')
    
        def query(self, sql_statement):
            query_result = []
            try:
                self.__connect()
                self.logger.info(('searching:{}'.format(sql_statement)))
                self.cursor.execute(sql_statement)
                query_result = self.cursor.fetchall()
                self.logger.info('searched')
                self.__disconnect()
            except Exception as ex:    
                line_number = ex.__traceback__.tb_lineno
                self.logger.info('[{}]:{}'.format(line_number,ex))
                raise Exception(ex)
    
            return query_result
    
        def insert(self, sql_statement, dataset):
            try:
                self.__connect()
                self.logger.info(('adding:{}'.format(sql_statement)))
                self.cursor.executemany(sql_statement, dataset)
                self.conn.commit()
                self.logger.info('added')
                self.__disconnect()
            except Exception as ex:   
                self.conn.rollback() 
                line_number = ex.__traceback__.tb_lineno
                self.logger.info('[{}]:{}'.format(line_number,ex))
                raise Exception(ex)
        
        def update(self, sql_statement):
            try:
                self.__connect()
                self.logger.info(('updating:{}'.format(sql_statement)))
                self.cursor.execute(sql_statement)
                self.conn.commit()
                self.logger.info('updated')
                self.__disconnect()
            except Exception as ex: 
                self.conn.rollback()    
                line_number = ex.__traceback__.tb_lineno
                self.logger.info('[{}]:{}'.format(line_number,ex))
                raise Exception(ex)
    
        def executeAll(self, sql_statement_list):
            try:
                self.__connect()
                for sql_statement in sql_statement_list:
                    self.logger.info(('prepareing:{}'.format(sql_statement)))
                    self.cursor.execute(sql_statement)
    
                self.conn.commit()
                self.logger.info('executed')
                self.__disconnect()
            except Exception as ex: 
                self.conn.rollback()    
                line_number = ex.__traceback__.tb_lineno
                self.logger.info('[{}]:{}'.format(line_number,ex))
                raise Exception(ex)
  • 相关阅读:
    关于SQL异地备份的方法(转)
    为VS2008添加OLE/COM 对象查看器
    Microsoft.SqlServer.Smo无法加载SqlServer.Management.Smo.Agent.JobBaseCollection解决方法(转)
    Python3+Pygame实现的射击游戏,很流畅,有音效
    Python项目第1集
    2021年Python3快速入门视频上线了,免费学!!!走起
    亲自动手实现Python+pygame中国象棋游戏
    人机对战版五子棋
    学生信息管理系统(Django入门级)
    Python3+pygame实现的flappy bird游戏,代码完整,还有音乐
  • 原文地址:https://www.cnblogs.com/lnd-blog/p/14291327.html
Copyright © 2011-2022 走看看