1.
the script to create the table from the structure is as follows
CreateTable Py_users (
ID int unsigned auto_increment NOT null primary key,
uname varchar is not NULL,
Upwd char () NOT NULL,
Is_delete bit NOT NULL default 0
);
Following the flowchart, the next code is written in this logic
2. Create the user_reg.py file with the following code
#coding =utf-8
From MySQLdb import*
From Hashlib IMPORTSHA1
If __name__== ' __main__ ':
Try
# 1. receive user input
Uname=raw_input (' Please enter user name: ')
Upwd=raw_input (' Please enter password: ')
#对密码加密
S1=SHA1 ()
S1.update (UPWD)
Upwd_sha1=s1.hexdigest ()
# 2. Open the connection to the database
Conn=connect (host= ' localhost ',
port=3306,database= ' python ', user= ' root ',
password= ' MySQL ', charset= ' UTF8 ')
Cur=conn.cursor ()
#判断用户名是否存在
Sql= ' SELECT COUNT (*) frompy_users where uname=%s '
Params=[uname]
Cur.execute (Sql,params)
Result=cur.fetchone ()
If result[0]==1:
print ' username already exists, registration failed '
Else
#用户名不存在
Sql= ' Insert intopy_users (UNAME,UPWD) VALUES (%s,%s) '
PARAMS=[UNAME,UPWD_SHA1]
Result=cur.execute (Sql,params)
Conn.commit ()
If result==1:
print ' registered successfully '
Else
print ' Registration failed '
Cur.close ()
Except Exception,e:
print ' registration failed because:%s '%e
Finally
Conn.close ()