#执行sql语句
import pymysql
conn = pymysql.connect(host='118.24.3.40',
port=3306,user='jxz',
password='123456',db='jxz',
charset='utf8',
autocommit = True)#自动提交
cur = conn.cursor(pymysql.cursors.DictCursor)#建立游标 #结果为key:value字典形式
sql = 'select * from app_myuser limit 5;'
sql2 = "insert into app_myuser set (username,password,isadmin) values ('yangjunchen','13904209244','819');"
cur.execute(sql)#执行sql语句
# conn.commit()#提交(update、delete、insert)
result = cur.fetchall()#获取执行结果(所有结果)
# result2 = cur.fetchone()#只能获取一条结果
print(result)
cur.close()
conn.close()
#执行select语句函数
def op_mysql(info,sql:str):
result = "执行完成"
conn = pymysql.connect(**info)
cur = conn.cursor(pymysql.cursors.DictCursor)
cur.execute(sql)
if sql.strip().lower().startswith('select'):
result = cur.fetchall()
cur.close()
conn.close()
return result
mysql_info = {
'host':'118.24.3.40',
'port':3306,
'password':'123456',
'user':'jxz',
'db':'jxz',
'charset':'utf8',
'autocommit':True
}
inert_sql = 'insert into app_myuser (username,passwd,is_admin) values ("abc123","sdfsdfs",1);'
select_sql ='select * from app_myuser where username="abc123";'
update_sql ='update app_myuser set passwd="123456" where username="abc123";'
delete_sql ='delete from app_myuser where username="abc123";'
result = op_mysql(mysql_info,delete_sql)
result = op_mysql(mysql_info,select_sql)
print(result)
#md5加密
import hashlib
#import md5 (python2里面直接引用)
s = '123456'
s = s.encode()#字符串转换为bytes(二进制)类型
m = hashlib.md5(s)#加密
result = m.hexdigest()#获取加密后的结果
print(result)#md5加密不可逆
#加盐
def md5(s,salt = '@@@'):
s = str(s+salt).encode()
m = hashlib.md5(s)
return m.hexdigest()