python连接mysql,redis类方法

时间:2021-06-15 17:22:48
#coding:utf8

import pymysql
import redis import conf '''MYSQL DB Object'''
class Mysql():
def __init__(self, dict=""):
parmas = {
'host' : conf.MYSQL_HOST,
'port' : conf.MYSQL_PORT,
'user' : conf.MYSQL_USER,
'password' : conf.MYSQL_PASS,
'db' : conf.MYSQL_DB,
'charset' : conf.MYSQL_CHARSET,
}
if dict:
parmas["cursorclass"] = pymysql.cursors.DictCursor self.conn = pymysql.connect(**parmas)
self.cursor = self.conn.cursor() def fetchone(self, query):
self.cursor.execute(query)
self.conn.commit()
return self.cursor.fetchone() def fetchall(self, query):
self.cursor.execute(query)
self.conn.commit()
return self.cursor.fetchall() def write(self, query):
write_count = self.cursor.execute(query)
self.conn.commit()
return write_count '''Redis DB Object'''
class Redis():
def __init__(self):
parmas = {
'host' : conf.REDIS_HOST,
'port' : conf.REDIS_PORT,
'password' : conf.REDIS_PASS,
}
self.cursor = redis.Redis(**parmas)