python自定义数据库工具类

python自定义数据库工具类

# -*- coding:utf-8 -*-

import pymysql
import pandas as pd
import pymysql.cursors
from sqlalchemy import create_engine
import io

class mysql:
    def __init__(self, host, port, dbuser, dbpwd, dbname):
      self.host = host
      self.port = port
      self.dbuser = dbuser
      self.dbpwd = dbpwd
      self.dbname = dbname
      self._conn = None
      self.Connect()   # 默认链接数据库, 最后要调用关闭链接的函数Con_close()函数

    def create_engine(self):
        db_url = ('{driver}://{user}:{pwd}@{host}:{port}/{name}?charset=utf8'.format(driver='mysql+pymysql', user=self.dbuser, port=self.port, pwd=self.dbpwd, host=self.host, name=self.dbname))
        self.engine = create_engine(db_url)

    def Connect(self):
        if not self._conn:
            # print('\033[33m 已创建数据库链接.......... \033[5m')
            self._conn = pymysql.connect(host=self.host,
                                         port=self.port,
                                         user=self.dbuser,
                                         passwd=self.dbpwd,
                                         db=self.dbname,
                                         use_unicode=True,
                                         charset='utf8',
                                         cursorclass=pymysql.cursors.DictCursor)    # cursorclass=pymysql.cursors.DictCursor
                                         #  MySQLdb默认查询结果都是返回tuple,输出时候不是很方便,必须按照0,1这样读取,
                                         # 无意中在网上找到简单的修改方法,就是传递一个cursors.DictCursor就行。
        else:
            print('\033[33m 链接数据库已存在,请直接操作!\033[5m')
            pass

    def Con_close(self):
        if self._conn:
            # print('\033[33m 数据库链接已关闭!!! \033[5m')
            self._conn.close()
            self._conn = None
            pass
        pass

    def NewCursor(self):
        cur = self._conn.cursor()
        if cur:
            return cur
        else:
            print('\033[33m #Error# Get New Cursor Failed \033[5m')
            return None
        pass

    def DelCursor(self, cur):
        if cur:
            cur.close()
            pass
        pass


    def Export(self, sql, file_name, colfg='\t\t'):
        """
        将查询结果导出到外部文件中
        :param sql:
        :param file_name:
        :param colfg:
        :return:
        """
        rt = self.Query(sql)
        if rt:
            with open(file_name, 'w') as fd:
                for row in rt:
                    in_info = ''
                    for col in row:
                        in_info += str(col) + colfg
                    in_info += '\n'
                    fd.write(in_info)


    def Query(self, sql, debug=0):
        """
        对数据库执行查询操作,sql为所需要执行的查询语句,debug控制是否显示查询的表结构(默认不查询即debug=0)
        :param sql:
        :param debug:
        :return:
        """
        rt = []
        cur = self.NewCursor()
        if not cur:
            return rt
        try:
            cur.execute(sql)
            while 1:
                ts = cur.fetchone()
                if ts is None:
                    break
                rt.append(ts)
            if debug:
                filename = []
                for field in cur.description:
                    filename.append(field[0])
                    print(filename)
                    # print('\033[33m Query Success!!! \033[5m')
        except Exception as err:
            print(err)
            print('\033[33m Exec Sql failed: "{0}" \033[5m'.format(sql))
        finally:
            self.DelCursor(cur)
            return rt



    def Exec(self, sql):
        """
        对数据库执行增、删、改操作,传入需要执行的SQL语句
        :param sql:
        :return:
        """
        rt = None
        cur = self.NewCursor()
        if not cur:
            return rt
        try:
            cur.execute(sql)
            # print('\033[33m update/delete/insert Success! \033[5m')
            self._conn.commit()
        except Exception as err:
            print('\033[33m Exec Sql Failed: "{0}" \033[5m'.format(sql))
            print(err)
            cur.rollback()
        finally:
            self.DelCursor(cur)
            return rt

    def dataframe(self,sql):
        """
        将sql语句读进pandas的数据框中
        :param sql:
        :return:
        """
        if self._conn:
            pass
        else:
            self.Connect()
        data=pd.read_sql(sql,self._conn)
        self.Con_close()
        return data

    def dataframe_to_sql(self, table, df, dbname='', style_fail_replace_append='append'):
        if dbname !="":
            self.dbname = dbname
        else:
            pass
        style = style_fail_replace_append
        engine = self.create_engine()
        # create_engine("mysql+mysqldb://root:123@localhost:3306/"+self.dbname +"?charset=utf8")
        output = io.StringIO()
        df.to_csv(output, index=None, encoding='utf_8_sig')
        output.seek(0)
        df1 = pd.read_csv(output)
        df1.to_sql(name=table, con=engine, if_exists=style, index=False, index_label=False,chunksize=10000000)

你可能感兴趣的:(技术)