1、Python3操作MySQL数据库需要安装一个第三方模块(pymysql):pip install pymysql;操作redis需要安装redis模块(redis):pip install redis
Python操作MySQL:
import pymysql #导入模块
# conn =pymysql.connect(host='211.149.147.233',user='byz',passwd='123456',db='byz',port=3306,charset='utf8')#创建数据库链接,指定IP、账号密码、端口、数据库名、字符集
# cur = conn.cursor(cursor=pymysql.cursors.DictCursor) #创建游标,指定输出数据类型
# cur= conn.cursor(cursor=pymysql.cursors.DictCursor)
# sql = 'insert into user (id,username,password) value (12,"test","123456");'
# sql1 = 'select * from user;'
# cur.execute(sql1) #执行sql
# print(cur.fetchall()) #获取SQL结果所有数据
# print(cur.fetchone()) #获取SQL结果一条数据
# print(cur.fetchone()) #获取SQL结果一条数据
# cur.scroll(4,mode='absolute') #和fetchone结合使用,将位置移动到第五(位置数是从0开始的)--绝对位置
# cur.scroll(4,mode='relative') #和fetchone结合使用,将位置往后移4(相对位置)
# print(cur.fetchone()) #结合absolute,这里显示数据库里面的第六条数据
# conn.commit() #提交执行 insert、delete、update必须 要提交才能生效
# cur.close() #关闭游标
# conn.close() #关闭数据库链接
# mysql_info = {
# "host":'211.149.147.233',
# "user":'byz',
# "passwd":'123456',
# "db":'byz',
# "port":3306,
# "charset":'utf8'
# }
#链接MySQL的函数
# def OpenMysql(sql):
# host=mysql_info['host']
# user=mysql_info['user']
# passwd=mysql_info['passwd']
# db=mysql_info['db']
# port=mysql_info['port']
# charset=mysql_info['charset']
# conn = pymysql.connect(host=host,user=user,passwd=passwd,db=db,port=port,charset=charset)
# cur= conn.cursor(cursor=pymysql.cursors.DictCursor)
# cur.execute(sql)
# if sql.startswith('select'):
# res = cur.fetchall()
# else:
# conn.commit()
# res = '提交成功'
# cur.close()
# conn.close()
# return res
# sql = 'insert into user (id,username,password) value (13,"test","123456");'
# sql = 'select * from user;'
#调用函数
rr = OpenMysql(sql=sql)
print(rr)
# Python操作redis
import redis,json
# r = redis.Redis(host='211.149.218.16',port=6379,db=0,password='123456') #链接redis
# r.set('name','jmy') #设置name的值,若name不存在就新建一个,否则就修改name的值
# r.set('name:xxx','jmy') #设置name的值,以文件夹方式 key=name:xxx value=jmy
# r.setnx('name','jmy') #设置name的值,name不存在的时候才会设置
# r.setex('name1','jmy',15) #设置name的值,超过15s即失效
# r.mset(name2='name2',name3='name3',name4='name4') #批量设置值
# print(r.mget('name','name1','name2','name3')) #批量获取key值
# r.delete('name') #删除值
# r.delete('name2','name3') #批量删除
#操作哈希类型的值
# r.hset('hname','key','value')
# r.hsetnx('hname','key2','value2') #给hname设置key和value值,key不存在的时候才会set
# r.hmset('hname',{'k1':'v1','k2':'v2'}) #给hname批量设置key value值
# print(r.hget('hname','key')) #获取这个hname里面指定的key值
# print(r.hgetall('hname')) #获取所有的key
# redis_info = {
# "host":'211.149.218.16',
# "passwd":'123456',
# "db":0,
# "port":6379
# }
# 操作redis的函数
# def Opredis(host,passwd,k,port=3679,db=0,v=False):
# r = redis.Redis(host=host,password=passwd,port=port,db=db) #链接redis
# if v: #如果v为空则调set,否则调用get
# r.set(k,v)
# res = '设置成功'
# else:
# res = r.get(k).decode() #返回获取值,这里要用decode将redis默认返回的byte字节转换成字符串
# return res
# #调用函数
# res = Opredis(
# host=redis_info['host'],
# passwd=redis_info['passwd'],
# db=redis_info['db'],
# port=redis_info['port'],
# k='zs'
# )
# print(res)
# MD5加密函数
import hashlib
def md5Password(password):
m = hashlib.md5()
m.update(bytes(password,encoding='utf-8')) #进行加密
new_passwd = m.hexdigest()#加密完的新字符
return new_passwd
# tt = md5Password('1212')
# print(tt)