import pymysql,redis def OpertioMsql(host,user,passwd,db,sql,port=3306,charset='utf8'): conn = pymysql.connect(host=host,user=user,passwd=passwd,port=port,db=db,charset=charset) #建立链接 cur = conn.cursor(cursor=pymysql.cursors.DictCursor) #建立游标 cur.execute(sql) #执行sql if sql.startswith('select'): res = cur.fetchall() else: conn.commit() res = 88 cur.close() conn.close() return res def OpRedis(host,passwd,k,v=False,port=6379,db=0): r = redis.Redis(host=host,password=passwd,port=port,db=db) #连接数据库 if v:#判断value是否传值,如果传了就说明是set方法 r.set(k,v) res = 88 else: res = r.get(k).decode() #因为从redis里面获取到数据都是字节类型的,要把它转成字符串 #所以要用decode方法 return res if __name__ == '__main__': sql = 'select * from user;' sql2="insert into user(id, username, password) VALUES(3, 'test2', 123)" # s = OpertioMsql(host='10.0.1.82',user='byz',passwd='123456',db='byz',sql=sql) # print(s) s = OpRedis('211.149.218.16','123456','nhy:word',) print(s)