要實現數據庫存儲用戶資料,首先要建立一個數據庫文件,並建立對應的表來存儲對應的用戶資料
我們先建立一個簡單的數據庫,里面只存儲了用戶的用戶名和密碼。
我們用python里自帶的sqlite3來建立一個數據庫
新建一個python文件:create_sql.py
代碼:----------------------------------------------------------------
#coding:utf-8 import sqlite3 #建一個數據庫 def create_sql(): sql = sqlite3.connect("user_data.db") sql.execute( """create table if not exists %s( %s integer primary key autoincrement, %s varchar(128), %s varchar(128))""" % ('user', 'id', 'name', 'passworld')) sql.close() return sql
#user_data.db為數據庫文件名
#create table if not exists為建立一個數據庫指令,如果文件存在就打開,不存在就創建一個
#%s對應后面的四個參數
# 'user':表名
# 'id':相當於一個索引,autoincrement指定為自動增量
# 'name',用戶名
# 'passworld'用戶密碼
# sql.close()是關閉數據庫,每次對數據庫進行操作后,都要記得進行關閉操作
create_sql()
運行
$python create_sql.py
運行成功后,在當前目錄下會生成"user_data.db"文件
就是我們要用到的數據庫文件,當然你也可以用sqlite3來生成這個文件
這個數據庫現在還不包含任何的用戶資料,如果用表格來表達這個數據庫大約就是如下
file:user_data.db
table:user
id name passworld
有了數據庫,我們就要向數據庫增加用戶資料
新建一個python
add_data.py
代碼:--------------------------------------------------------------------------------
#coding:utf-8 import sqlite3 #數據庫增加數據 def add_data(): sql = sqlite3.connect("user_data.db") sql.execute("insert into user(name,passworld) values('python','147258369')") sql.commit() print "添加成功" sql.close() # 關閉數據庫 #sqlite3.connect("user_data.db")要增加一條用戶資料,首先要打開數據庫文件 #"insert into user(name,passworld) values('python','147258369')"是sqlite3指令 #它表示在表'user'的name對應的列寫入一行python,就是用戶名,在passworld對應的列寫入一行147258369,就是用戶密碼 #做完這一步之后,對數據庫的操作已經完成,但user_data.db這個文件是還沒有發生變化的 #sql.commit()提交任務,只有使用了commit()方法后,才會把之前對數據庫的操作寫入文件中, # sql.close()是關閉數據庫,每次對數據庫進行操作后,都要記得進行關閉操作 add_data()
這里不用運行這段代碼,這里只是為說明如何在數據庫中增加數據
這段代碼過於簡單,如果我們想要自定義輸入用戶名和密碼就只能修改代碼內的
values('python','147258369')字段,比較不方便
所以,為了以后方便地對數據庫進行增加和查詢,在上面的基礎上,另外建立一個python
data_operations.py
代碼:---------------------------------------------------------------------
#coding:utf-8 import sqlite3 #數據庫增加數據 def add_data(): input_name = str(raw_input("請輸入您的用戶名:")) input_passworld = str(raw_input("請輸入您的密碼:")) sql = sqlite3.connect("user_data.db") sql.execute("insert into user(name,passworld) values(?,?)", (input_name,input_passworld)) sql.commit() print "添加成功" sql.close() #這里增加了讀取兩個參數values(?,?)字段里的兩個?號,對應了參數input_name,input_passworld def showalldata(): sql = sqlite3.connect("user_data.db") data=sql.execute("select * from user").fetchall() sql.close() return data #"select * from user"為查詢user對應的表的所有資料的指令 while 1: option= """ 1:增加數據 2:查詢數據 q:退出 選擇您想要的進行的操作:""" cho = raw_input(option) if cho=='1': add_data() elif cho=='2': showalldata() elif cho=='q': break else: "輸入錯誤"
我們現在要做的是實現注冊一個新的用戶,並用戶名只能為唯一
比如,這個假定的數據庫已經有一個用戶,名為python,當我們新注冊用同樣的用戶名時,會要求重新輸入
建立python文件:register.py
代碼:--------------------------------------------------------
#coding:utf-8 import sqlite3 #數據庫增加數據 def register(): while 1: input_name = str(raw_input("請輸入您的用戶名:")) sql = sqlite3.connect("user_data.db") data = sql.execute("select * from user where name='%s'" % input_name).fetchone() if not data: input_passworld = str(raw_input("請輸入您的密碼:")) sql.execute("insert into user(name,passworld) values(?,?)", (input_name,input_passworld)) sql.commit() print "添加成功" sql.close() break else: print "用戶已存在" register() 運行: $python register.py 請輸入您的用戶名:python 用戶已存在 請輸入您的用戶名:py 請輸入您的密碼:147258 添加成功
那么,有了數據庫,前面的工作只是建立了對應的數據庫,更多的對數據庫的操作,下一篇博文會更詳細地說明
里面也已保存有我們的用戶名和密碼了,下面我們進行一個簡單的登錄操作
建立python文件
login.py
代碼--------------------------------------------------------------------------
#coding:utf-8 import sqlite3,getpass def showdate(username): sql = sqlite3.connect('user_data.db') data = sql.execute("select * from user where name='%s'"% username).fetchone() sql.close() return data #("select * from user where username='%s'"% name)這條指令用來查詢表user的name字段的對應的數據 #最后返回用戶名對應的資料 def val(): while 1: name = raw_input("用戶名")#輸入用戶名 data = showdate(name)#獲取用戶名對應的數據庫資料 if data: passworld = getpass.getpass("密碼") # 輸入密碼 if data[2] == passworld: print "登錄成功" break else: print"密碼錯誤" else: print"用戶名錯誤" #getpass模塊可以使輸入的字符不可見,在python的shell中可能無效,在終端中和用戶登錄效果相同,不出現輸入字符 val() print "進入用戶操作"
$python login.py
這時會提示輸入用戶名和密碼,輸入錯誤將一直循環,直到輸入正確用戶名和密碼。
這當然不是我們要的最終結果,我們還會要加入比如多次輸入錯誤鎖定用戶,那么就要在數據庫中存更多的數據
比如,錯誤次數字段,是否鎖定狀態的字段,鎖定時間字段(方便一定時間后自動解鎖),錯誤次數字段
當然,我們也可以加入用戶地址,電話,郵箱等更多資料,不過這里只為了實現登錄的操作,這些旁支也不在這里詳述了