Python結(jié)合MySQL數(shù)據(jù)庫編寫簡單信息管理系統(tǒng)完整實例
1,項目整體邏輯及使用工具
1.1 項目整體邏輯
本項目主要是使用Python進行編寫,利用Python中的pymysql庫進行連接數(shù)據(jù)庫,將信息存入MySQL數(shù)據(jù)庫中,然后實現(xiàn)對信息進行增刪改查等一系列操作。
1.2 使用工具
(1):使用pymysql庫
(2):python 3.9
(3):MySQL 8.0
1.3 pymysql庫的安裝
pip install pymysql
2,數(shù)據(jù)庫的搭建
2.1本項目為簡單的信息管理系統(tǒng)的實現(xiàn)
創(chuàng)建數(shù)據(jù)庫一個六個字段分如下:
2.2數(shù)據(jù)庫搭建代碼
create table if not exists information( pid int primary key AUTO_INCREMENT, -- 主鍵 users varchar(20) not null , -- 賬號 cod varchar(20), -- 密碼 name varchar(20), -- 姓名 age int, -- 年齡 mobile varchar(50) -- 電話號碼 );
3,Python代碼編寫
3.1 使用pymysql進行連接數(shù)據(jù)庫
import pymysql conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123456' ) cus = conn.cursor() # 創(chuàng)建游標 sql="" # 編寫sql語句 cus.execute(sql) #使用游標執(zhí)行sql語句 conn.commit() #進行提交 cus.close() #關閉游標 conn.close() #關閉數(shù)據(jù)庫連接
3.2 系統(tǒng)界面進行編寫
print('*' * 54) print('【1】注冊用戶信息') print('【2】刪除用戶信息') print('【3】修改用戶信息') print('【4】查詢用戶信息') print('【5】退出系統(tǒng)') print('*' * 54) n = input('請輸入你要執(zhí)行的命令') if n == '1': register(cus,conn) elif n == '2': strike(cus, conn) elif n == '3': modify(cus, conn) elif n == '4': inquiry(cus,conn) elif n == '5': cus.close() #關閉游標 conn.close() #關閉數(shù)據(jù)庫連接 break else: print('輸入錯誤請重新輸入')
3.3 對用戶注冊模塊進行編寫
def register(cus,conn): # 注冊模塊 users=input('請輸入用戶賬號') cod=input('請輸入用戶密碼') name=input('請輸入用戶姓名') age=int(input('請輸入用戶年齡')) mobile=input('請輸入用戶的手機號') sql=f"insert into xinxi.information(users,cod,name,age,mobile) values ('{users}','{cod}','{name}',{age},'{mobile}')" cus.execute(sql) conn.commit() print('注冊成功') pass
3.4 對用戶信息刪除模塊進行編寫
def strike(cus,conn): #刪除用戶 users = input('請輸入需要刪除的用戶賬號') cod = input('請輸入密碼') sql = f"select * from xinxi.information where users='{users}' and cod='{cod}'" n = cus.execute(sql) # conn.commit() # 提交信息 # print(n) if n: sql =f"delete from xinxi.information where users='{users}' and cod='{cod}'" cus.execute(sql) conn.commit() print('刪除成功') else: print('查無此人')
3.5 對用戶信息修改模塊進行編寫
def modify(cus,conn): #修改信息 users=input('請輸入需要修改的用戶賬號') cod=input('請輸入密碼') sql=f"select * from xinxi.information where users='{users}' and cod='{cod}'" n=cus.execute(sql) #conn.commit() # 提交信息 #print(n) if n: users1 = input('請輸入需要修改的用戶賬號') cod1 = input('請輸入需要修改用戶密碼') name = input('請輸入需要修改用戶姓名') age = int(input('請輸入需要修改用戶年齡')) mobile = input('請輸入需要修改用戶的手機號') sql=f"update xinxi.information set users='{users1}',cod='{cod1}',name='{name}',age={age},mobile='{mobile}' where users='{users}' and cod='{cod}'" cus.execute(sql) conn.commit() print('修改成功') else: print('查無此人')
3.6 對用戶信息查詢模塊進行編寫
def inquiry(cus,conn): #查詢信息 users = input('請輸入需要查詢的用戶賬號') cod = input('請輸入密碼') sql = f"select * from xinxi.information where users='{users}' and cod='{cod}'" n = cus.execute(sql) if n: sql = f"select name,age,mobile from xinxi.information where users='{users}' and cod='{cod}'" cus.execute(sql) #接收數(shù)據(jù) u=cus.fetchall() #conn.commit() print('用戶的姓名為:',u[0][0]) print('用戶的年齡為:',u[0][1]) print('用戶的電話為',u[0][2]) else: print('查無此人')
4,系統(tǒng)整體代碼
# 需求,登陸后會用戶進行查詢 import pymysql def main(): conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123456' ) cus = conn.cursor() # 創(chuàng)建 while True: print('*' * 54) print('【1】注冊用戶信息') print('【2】刪除用戶信息') print('【3】修改用戶信息') print('【4】查詢用戶信息') print('【5】退出系統(tǒng)') print('*' * 54) n = input('請輸入你要執(zhí)行的命令') if n == '1': register(cus,conn) elif n == '2': strike(cus, conn) elif n == '3': modify(cus, conn) elif n == '4': inquiry(cus,conn) elif n == '5': cus.close() #關閉游標 conn.close() #關閉數(shù)據(jù)庫連接 break else: print('輸入錯誤請重新輸入') def register(cus,conn): # 注冊模塊 users=input('請輸入用戶賬號') cod=input('請輸入用戶密碼') name=input('請輸入用戶姓名') age=int(input('請輸入用戶年齡')) mobile=input('請輸入用戶的手機號') sql=f"insert into xinxi.information(users,cod,name,age,mobile) values ('{users}','{cod}','{name}',{age},'{mobile}')" cus.execute(sql) conn.commit() print('注冊成功') pass def strike(cus,conn): #刪除用戶 users = input('請輸入需要刪除的用戶賬號') cod = input('請輸入密碼') sql = f"select * from xinxi.information where users='{users}' and cod='{cod}'" n = cus.execute(sql) # conn.commit() # 提交信息 # print(n) if n: sql =f"delete from xinxi.information where users='{users}' and cod='{cod}'" cus.execute(sql) conn.commit() print('刪除成功') else: print('查無此人') def modify(cus,conn): #修改信息 users=input('請輸入需要修改的用戶賬號') cod=input('請輸入密碼') sql=f"select * from xinxi.information where users='{users}' and cod='{cod}'" n=cus.execute(sql) #conn.commit() # 提交信息 #print(n) if n: users1 = input('請輸入需要修改的用戶賬號') cod1 = input('請輸入需要修改用戶密碼') name = input('請輸入需要修改用戶姓名') age = int(input('請輸入需要修改用戶年齡')) mobile = input('請輸入需要修改用戶的手機號') sql=f"update xinxi.information set users='{users1}',cod='{cod1}',name='{name}',age={age},mobile='{mobile}' where users='{users}' and cod='{cod}'" cus.execute(sql) conn.commit() print('修改成功') else: print('查無此人') def inquiry(cus,conn): #查詢信息 users = input('請輸入需要查詢的用戶賬號') cod = input('請輸入密碼') sql = f"select * from xinxi.information where users='{users}' and cod='{cod}'" n = cus.execute(sql) if n: sql = f"select name,age,mobile from xinxi.information where users='{users}' and cod='{cod}'" cus.execute(sql) #接收數(shù)據(jù) u=cus.fetchall() #conn.commit() print('用戶的姓名為:',u[0][0]) print('用戶的年齡為:',u[0][1]) print('用戶的電話為',u[0][2]) else: print('查無此人') if __name__ == '__main__': main()
使用本程序需要安裝MySQL數(shù)據(jù)庫并創(chuàng)建數(shù)據(jù)庫。
總結(jié)
到此這篇關于Python結(jié)合MySQL數(shù)據(jù)庫編寫簡單信息管理系統(tǒng)的文章就介紹到這了,更多相關Python MySQL編寫信息管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python如何將給定字符串中的大寫英文字母按以下對應規(guī)則替換
這篇文章主要介紹了Python如何將給定字符串中的大寫英文字母按以下對應規(guī)則替換,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10PyCharm最新激活碼(2020/10/27全網(wǎng)最新)
Pycharm最新激活碼全網(wǎng)最新(2020/10/27更新),適用Intellij idea 2020.2.x,WebStorm 2020.2.x,Pycharm 2020.2.x2020-10-10淺談Django自定義模板標簽template_tags的用處
這篇文章主要介紹了淺談Django自定義模板標簽template_tags的用處,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12wxpython+pymysql實現(xiàn)用戶登陸功能
這篇文章主要介紹了wxpython+pymysql實現(xiàn)用戶登陸功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11