python-mysql用户注册

import pymysql
import hashlib
config={
    'host':"localhost",
    'port':3306,
    'user':"root",
    'passwd':"HelloWorld",
    'db':"lianxi",
    'charset':"utf8"
}

while True:
    try:
        conn = pymysql.connect(**config)
        cus1 = conn.cursor()
    except Exception:
        print("数据库连接异常,请检查数据库连接配置")
        exit()
    name = input("用户名:")
    cus1.execute("SELECT * FROM test WHERE name = '%s'"%(name))
    if len(cus1.fetchall())==0:
        break
    else:
        print("此用户已注册")
while True:
    pwd1 = input("密码:")
    pwd2 = input("请再输入一遍密码:")
    if pwd1 == pwd2:
        break
try:
    sql = "insert into test(name,password) values(%s,%s)"
    pwd = hashlib.sha1(pwd2.encode("utf-8")).hexdigest()
    data = [name,pwd]
    cus1.execute(sql,data)
    conn.commit()
    cus1.close()
    conn.close()
except Exception as e:
    print(e)
else:
    print("用户注册成功!")
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容