Database instance: User Registers __ Database

Source: Internet
Author: User
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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.