作業就是寫一個簡單 的圖書信息管理系統,實現增刪改查功能,附帶了登錄注冊功能,就是把上一次的代碼直接拿來用了。如下:
主函數:main.py
import pymysql import getpass import time from operate import manage #查詢用戶名是否存在 def check(uname): namelist = [] conn1 = pymysql.connect("localhost", "root", "admin", "login", charset='utf8') cur = conn1.cursor() sql1 = "select uName from logintable" cur.execute(sql1) results = cur.fetchall() for row in results: namelist.append(row[0]) i = 0 for x in namelist: if (uname == x): i = 1 break return i # 注冊函數 def register(): i = 1 username = "" passwd = "" while i == 1: username = input("請輸入用戶名:") if (check(username) == 1): print('該用戶名已被占用,請重新輸入') continue if (username.isalnum()): break else: print("用戶名只能包含數字與英文字母!!!") while i == 1: passwd = getpass.getpass("請輸入密碼:") if (len(passwd) > 5 and len(passwd) < 21): break else: print("密碼為6至20位!!!") pswd = getpass.getpass('請再輸入一遍密碼:') if (passwd == pswd): print('注冊成功!') else: print('兩次密碼不相同,請重新注冊!!!') register() # 打開數據庫連接 conn = pymysql.connect("localhost", "root", "admin", "login", charset='utf8') # 使用corsor()方法創建一個游標對象 cursor = conn.cursor() # 使用execute()方法執行sql語句,將注冊的信息插入到數據庫中 sql = "INSERT INTO logintable (uName,pw) VALUES (%s,%s)" val = (username, passwd) # 這兩行執行sql語句 cursor.execute(sql, val) conn.commit()#提交數據庫執行 cursor.close() conn.close() # 返回主界面 first() # 登錄函數 def login(): sql = "select * from logintable" conn = pymysql.connect("localhost", "root", "admin", "login", charset='utf8') cursor = conn.cursor() cursor.execute(sql) result = cursor.fetchall() i = 1 s = 0 while i == 1: uname = input('請輸入用戶名:') if (check(uname) == 0): print('該用戶不存在!!!') continue else: while s < 3: passwd = getpass.getpass("請輸入密碼:") key = 0 for row in result: if (uname == row[0] and passwd == row[1]): key = 1 if (key == 1): print('登錄成功!!!') break else: print('密碼錯誤!請重新登錄!') s = s + 1 if s == 3: print('賬戶已鎖定!!!請 5 s 后重新登錄!!!') time.sleep(5) break manage() #主界面函數 def first(): print('--------------xxx圖書信息管理系統------------') print('輸入序號,進行選擇:') print('1、注冊') print('2、登錄') print('3、退出') select = input('請選擇: ') if select == '1': register() elif select == '2': login() elif select == '3': exit(0) else: print('請輸入正確序號:') first() first()
增刪改查模塊 :operate.py
import pymysql #from sqlConnect import addToMysql,check_bookname,check_author from mysqlOperate import* import mysqlOperate #管理函數 def manage(): m=1 while m == 1: print("-----------------------圖書管理----------------------") print("1、查詢圖書信息") print("2、添加圖書信息") print("3、刪除圖書信息") print("4、修改圖書信息") print("5、退出") select = input("請輸入編號選擇功能:") if select == '1': check() elif select == '2': add() elif select == '3': delete() elif select == '4': update() elif select == '5': exit(0) else: print("請輸入正確序號!!!") #添加函數 def add(): print("請輸入圖書信息:") bookname=input("請輸入書籍名稱:") author=input('請輸入作者姓名:') price=input('請輸入價格:') addToMysql(bookname,author,price) #查詢函數 def check(): print("-----------------------查詢---------------------") print("請選擇:") print("1、按書名查詢 2、按作者查詢 3、查詢所有圖書 (其他任意鍵返回)") select=input('請輸入編號:') if select=='1': bookname = input("請輸入書名:") check_bookname(bookname) elif select=='2': authorname=input("請輸入作者姓名:") check_author(authorname) elif select=='3': check_all() #刪除函數 def delete(): print("-----------------------刪除----------------------") bookname=input("請輸入要刪除的書名:") check_bookname(bookname) select=input("確定刪除?(請輸入y/n):") if select=='y': delete_book(bookname) elif select=='n': print("未刪除!!!") else: print("輸入錯誤!!已退出!!!") #修改函數 def update(): print("-----------------------修改------------------------") bookname=input("請輸入要修改的書名:") check_bookname(bookname) select=input('確定修改?(y/n):') if select=='n': print('已返回') elif select=='y': update_book(bookname)
數據庫操作模塊:mysqlOperate.py
import pymysql #創建數據庫連接 conn=pymysql.connect("localhost","root","admin","login",charset='utf8') #創建游標對象 cursor=conn.cursor() #添加圖書 def addToMysql(name,author,price): str="INSERT INTO bookinfo (bookname,author,price) VALUES (%s,%s,%s)" val=(name,author,price) cursor.execute(str,val) conn.commit() print("添加成功") #按書名查詢 def check_bookname(bookName): cursor.execute('SELECT * FROM bookinfo WHERE bookname = %s',(bookName,)) result=cursor.fetchall() i = 0 if result==None: print ('該書不存在!!!') else: for row in result: print("-------第%s本書-------" % i) print("book_id:", row[0]) print("書名:", row[1]) print("作者:", row[2]) print("價格:", row[3]) i = i + 1 # 按作者查詢 def check_author(author): cursor.execute('SELECT * FROM bookinfo WHERE author = %s',(author,)) result=cursor.fetchall() i=1 if result==None: print('該作者不存在!!!') else: for row in result: print('-------第%s本書--------'% i) print("book_id:", row[0]) print("書名:", row[1]) print("作者:", row[2]) print("價格:", row[3]) i = i + 1 #查詢所有圖書 def check_all(): cursor.execute('SELECT * FROM bookinfo') result=cursor.fetchall() i=1 for row in result: print('-------第%s本書--------' % i) print("book_id:", row[0]) print("書名:", row[1]) print("作者:", row[2]) print("價格:", row[3]) i = i + 1 # 刪除書籍 def delete_book(bookname): cursor.execute('DELETE FROM bookinfo WHERE bookname = %s' ,(bookname,)) conn.commit() print('刪除成功!!!') #修改書籍信息 def update_book(bookname): new_bookname=input('請輸入修改書名:') new_author=input('請輸入修改作者名稱:') new_price=input('請輸入修改價格:') cursor.execute('UPDATE bookinfo SET bookname=%s,author=%s,price=%s WHERE bookname=%s',(new_bookname,new_author,new_price,bookname)) conn.commit() print('修改成功')
數據庫設計:
bookinfo表
logintable表:
另外,執行時跟上一次作業一樣,通過cmd執行