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("用户注册成功!")

你可能感兴趣的:(python-mysql用户注册)