db.py 3.24 KB
Newer Older
taoke committed
1
import redis,pymysql
2
from config import BaseConfig
3 4 5 6

# redis类
class RedisBase:
    def __init__(self, inum):
7
        test_redis = BaseConfig().test_redis
8 9
        """每一个数据库实例管理一个连接池"""
        self.num = inum
10
        pool = redis.ConnectionPool(host=test_redis['host'], port=test_redis['port'], db=self.num, password=test_redis['password'])
11 12 13 14 15 16 17 18 19
        self.r = redis.Redis(connection_pool=pool)

class RedisString(RedisBase):
    def __init__(self, inum):
        RedisBase.__init__(self, inum=inum)
    def get(self, xx):
        """获取值"""
        result = self.r.get(xx)
        return result
taoke committed
20
    def delete_key(self,xxx):
21 22 23 24 25
        try:
            self.r.delete(*self.r.keys(f'{xxx}*'))
        except:
            pass

taoke committed
26

27 28


taoke committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
# mysql类
class MYSQL:
    """
    对pymysql的简单封装
    """
    def __init__(self, host, port,user, pwd, db):
        self.host = host
        self.port = port
        self.user = user
        self.pwd = pwd
        self.db = db

    def __GetConnect(self):
        """
        得到连接信息
        返回: conn.cursor()
        """
        self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user,
                                    password=self.pwd, database=self.db, charset="utf8")
48
        cur = self.conn.cursor(pymysql.cursors.DictCursor)
taoke committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        return cur

    def ExecuQuery(self,sql):
        """
        执行查询语句
        返回的是一个包含tuple的list,list的元素是记录行,tuple的元素是每行记录的字段

        调用示例:
                ms = MYSQL(host="localhost",user="sa",pwd="123456",db="PythonWeiboStatistics")
                resList = ms.ExecQuery("SELECT id,NickName FROM WeiBoUser")
                for (id,NickName) in resList:
                    print str(id),NickName
        """
        cur = self.__GetConnect()
        cur.execute(sql)
        resList = cur.fetchall()        #查询所有,得到列表 [(1,2,3),(1,2,3),(1,2,3)]
        # resList = cur.fetchone()      # 查询一个,得到1个(不用这个)

        #查询完毕后必须关闭连接
        self.conn.close()
        return resList

    def ExecuNonQuery(self,sql):
        """
        执行非查询语句

        调用示例:
            cur = self.__GetConnect()
            cur.execute(sql)
            self.conn.commit()
            self.conn.close()
        """
        cur = self.__GetConnect()
        cur.execute(sql)
        self.conn.commit()
        self.conn.close()


87 88 89


if __name__ == '__main__':
taoke committed
90
    # r= RedisString(6).get('bmc:captcha:40ec9359-a0e8-42a1-b0c0-c19f199cab60')
taoke committed
91
    # r = RedisString(0).get('edl:sms_value:17822000010:MOBILE_REGISTER')
92 93
    # print(r)
    # print(str(r)[-7:-1])
taoke committed
94
    # pass
95
    # RedisString(0).delete_key("bmc:c1:dl_img:uid")
taoke committed
96
    mysql_config=("10.197.236.190", 3306, "root", "123456", "")
fanxun committed
97
    # mysql = MYSQL(host="10.197.236.190", port=3306, user="root", pwd="123456", db="edl_private")
taoke committed
98
    mysql = MYSQL(*mysql_config)
99
    # info = mysql.ExecuQuery("SELECT * FROM db_tbd_base1.project;")
100
    info = mysql.ExecuQuery( "select id from db_tbd.base_transport_driver where license_number='510122202011120001';")
101

taoke committed
102
    print(info)
103
    print(info[0]["id"])
taoke committed
104

taoke committed
105
    # r = RedisString(0).get('bmc:captcha:1ad7d0e9-1e01-454c-8500-d7b5b15c90ff')