def __init__(self):
     self.con = pymysql.connect(host=conf.get('mysql', 'host'),
                                port=conf.getint('mysql', 'port'),
                                user=conf.get('mysql', 'user'),
                                password=conf.get('mysql', 'password'),
                                charset='utf8'
                                # cursorclass=pymysql.cursors.DictCursor
                                )
Beispiel #2
0
    def __init__(self):
        try:
            self.conn = pymysql.connect(host=conf.get('mysql', 'host'),
                                        user=conf.get('mysql', 'user'),
                                        password=conf.get('mysql', 'password'),
                                        database=conf.get('mysql', 'db'),
                                        port=conf.getint('mysql', 'port'),
                                        charset='utf8',
                                        cursorclass=pymysql.cursors.DictCursor)

            # 建立游标
            self.cur = self.conn.cursor()

        except:
            print("连接数据库失败!")
            raise
    def find_count(self, sql):
        # sql 执行完后返回的数据条数
        with self.con as cur:
            res = cur.execute(sql)
        cur.close()
        return res

    def __del__(self):
        self.con.close()


if __name__ == '__main__':
    sql = 'select * FROM futureloan.member LIMIT 5;'
    db1 = HandleDB(
        host=conf.get('mysql', 'host'),
        port=conf.getint('mysql', 'port'),
        user=conf.get('mysql', 'user'),
        password=conf.get('mysql', 'password')
    )
    db2 = HandleDB(
        host=conf.get('mysql2', 'host'),
        port=conf.getint('mysql2', 'port'),
        user=conf.get('mysql2', 'user'),
        password=conf.get('mysql2', 'password')
    )
    res = db1.find_one(sql)
    # res=db.find_count(sql)
    # res = db.find_all(sql)
    print(res)
    # del db
    ============
    Author:hw
    data:2020/11/6 12:00
    ============
"""
import pymysql
from common.handle_conf import conf


class Db:
    def __init__(self, host, port, user, password):
        self.con = pymysql.Connect(host=host,
                                   port=port,
                                   user=user,
                                   password=password,
                                   charset="utf8",
                                   cursorclass=pymysql.cursors.DictCursor)
        self.cur = self.con.cursor()

    def find_data(self, sql):
        self.con.commit()
        self.cur.execute(sql)
        data = self.cur.fetchall()
        return data


db = Db(host=conf.get("mysql", "host"),
        port=conf.getint("mysql", "port"),
        user=conf.get("mysql", "user"),
        password=conf.get("mysql", "password"))