教你用python實(shí)現(xiàn)一個(gè)無(wú)界面的小型圖書(shū)管理系統(tǒng)
一、需求了解
功能模塊
圖書(shū)信息
二、環(huán)境準(zhǔn)備
安裝mysql數(shù)據(jù)庫(kù)
參考文章:
MySQL數(shù)據(jù)庫(kù)壓縮版本安裝與配置
創(chuàng)建數(shù)據(jù)庫(kù)表
- 創(chuàng)建數(shù)據(jù)庫(kù)
CREATE DATABASE bookmanage;
- 使用數(shù)據(jù)庫(kù)
use bookmanage;
- 創(chuàng)建表
create table books(
id int unsigned primary key auto_increment not null,
name varchar(20) default “”,
position varchar(40) default “”,
status enum(‘在庫(kù)', ‘出借') default ‘在庫(kù)',
borrower varchar(20) default “”
);
- 插入數(shù)據(jù)
insert into books(name, position) value (‘python從入門(mén)到放棄', ‘A-1-1');
- 查詢數(shù)據(jù)
select * from books where id=2;
- 修改數(shù)據(jù)
update books set name=‘python';
- 刪除數(shù)據(jù)
delete from book where id=3;
三、代碼實(shí)現(xiàn)
引入pymysql模塊
- 安裝pymysql
命令:pip install pymysql
- 封裝操作數(shù)據(jù)庫(kù)模塊
# -*- coding: utf-8 -*- """ =============================== @Time : 2021/5/18 15:56 @Author : flora.chen @FileName: handle_mysql.py @Software: PyCharm =============================== """ import pymysql class MysqlDB: """ 操作mysql數(shù)據(jù)庫(kù) """ def __init__(self, host, user, pwd, database=None, port=3306): """ 初始化數(shù)據(jù)庫(kù)鏈接 :param host: 主機(jī)地址 :param user: 用戶名 :param pwd: 密碼 :param database: 數(shù)據(jù)庫(kù)名稱,默認(rèn)為空 :param port: 端口號(hào),默認(rèn)3306 """ self.conn = pymysql.connect( host=host, user=user, password=pwd, database=database, port=port, cursorclass=pymysql.cursors.DictCursor ) # 創(chuàng)建一個(gè)游標(biāo)對(duì)象 self.cur = self.conn.cursor() def update(self, sql): """ 進(jìn)行增刪改操作 :param sql: 需要執(zhí)行的SQL :return: """ # 執(zhí)行SQL result = self.cur.execute(sql) # 提交事務(wù) self.conn.commit() return result def query(self, sql, one=False): """ 進(jìn)行查詢操作 :param one: 判斷是要返回所有查詢數(shù)據(jù)還是第一條,默認(rèn)是所有 :param sql: 要執(zhí)行的SQL :return: """ # 執(zhí)行SQL self.cur.execute(sql) if one: return self.cur.fetchone() else: return self.cur.fetchall() def close(self): """ 斷開(kāi)游標(biāo),關(guān)閉數(shù)據(jù)庫(kù)連接 :return: """ self.cur.close() self.conn.close() if __name__ == "__main__": db = MysqlDB(host="localhost", user="root", pwd="root") print(db.query("select * from bookmanage.books")) # db.update("insert into bookmanage.books(name, position) value ('python從入門(mén)到放棄', 'A-1-1');")
圖案管理系統(tǒng)后臺(tái)實(shí)現(xiàn)
# -*- coding: utf-8 -*- """ =============================== @Time : 2021/5/18 16:39 @Author : flora.chen @FileName: bookmanager.py @Software: PyCharm =============================== """ from handle_mysql import MysqlDB db = MysqlDB(host="localhost", database="bookmanage", user="root", pwd="root") class BookManage: """ 圖書(shū)管理系統(tǒng) """ @staticmethod def print_menu(): """ 菜單打印 :return: """ print("---------------------菜單-------------------------") print("[1]: 添加圖書(shū)") print("[2]: 修改圖書(shū)") print("[3]: 刪除圖書(shū)") print("[4]: 查詢圖書(shū)") print("[5]: 圖書(shū)列表") print("[6]: 出借圖書(shū)") print("[7]: 歸還圖書(shū)") print("[8]: 退出") def add_book(self): """ [1]: 添加圖書(shū) :return: """ print("****************添加圖書(shū)****************") name = input("請(qǐng)輸入書(shū)名:") position = input("請(qǐng)輸入圖書(shū)位置:") if name and position: db.update("insert into books(name, position) value ('{}', '{}');".format(name, position)) print("圖書(shū)添加成功") else: print("書(shū)名或者圖書(shū)位置不能為空,請(qǐng)重新輸入!") num = input("繼續(xù)添加請(qǐng)輸入1, 回車退回主菜單") if num == "1": self.add_book() def update_book(self): """ [2]: 修改圖書(shū) :return: """ print("****************修改圖書(shū)****************") book_id = input("請(qǐng)輸入需要修改的圖書(shū)ID:") result = db.query("select * from books where id={};".format(book_id), one=True) if result: print("當(dāng)前數(shù)據(jù)為:{}".format(result)) name = input("重新輸入書(shū)名,不修改輸入回車:") or result["name"] position = input("重新輸入位置,不修改輸入回車:") or result["position"] db.update("update books set name='{}', position='{}' where id={};".format(name, position, book_id)) print("修改成功") else: print("您輸入的圖書(shū)ID不存在,請(qǐng)重新輸入~") num = input("繼續(xù)修改請(qǐng)輸入1, 回車退回主菜單") if num == "1": self.update_book() def delete_book(self): """ [3]: 刪除圖書(shū) :return: """ print("****************刪除圖書(shū)****************") book_id = input("請(qǐng)輸入需要修改的圖書(shū)ID:") result = db.query("select * from books where id={};".format(book_id), one=True) if result: print("當(dāng)前數(shù)據(jù)為:{}".format(result)) confirm_num = input("確定需要?jiǎng)h除這本書(shū)嗎?確認(rèn)請(qǐng)按1,取消請(qǐng)按2:") if confirm_num == "1": db.update("delete from books where id={};".format(book_id)) print("刪除成功") else: print("已確認(rèn)不刪除該書(shū)籍~") else: print("系統(tǒng)中未找到該書(shū)籍!") num = input("繼續(xù)刪除請(qǐng)輸入1, 回車退回主菜單") if num == "1": self.delete_book() def query_book(self): """ [4]: 查詢圖書(shū) :return: """ print("****************查詢圖書(shū)****************") name = input("請(qǐng)輸入您要查詢的圖書(shū)名稱(模糊匹配):") if name: result = db.query("select * from books where name like '%{}%';".format(name)) if result: print("當(dāng)前查詢到如下書(shū)籍信息:{}".format(result)) else: print("未查詢到相關(guān)書(shū)籍信息~") else: print("書(shū)名不能為空!") num = input("繼續(xù)查詢請(qǐng)輸入1, 回車退回主菜單") if num == "1": self.query_book() def book_list(self): """ [5]: 圖書(shū)列表 :return: """ print("****************圖書(shū)列表****************") result = db.query("select * from books;") for i in result: print("編號(hào):{}, 書(shū)籍名:{}, 位置:{}, 狀態(tài):{}, 借閱人:{}".format(i["id"], i["name"], i["position"], i["status"], i["borrower"])) def borrow_book(self): """ [6]: 出借圖書(shū) :return: """ print("****************出借圖書(shū)****************") book_id = input("請(qǐng)輸入需要借閱的圖書(shū)ID:") result = db.query("select * from books where id={};".format(book_id), one=True) if result: if result["status"] == "出借": print("抱歉,該書(shū)已經(jīng)借出!") else: while True: borrower = input("請(qǐng)輸入借閱者的名字:") if borrower: db.update("update books set borrower='{}' where id={};".format(borrower, book_id)) db.update("update books set status='出借' where id={};".format(book_id)) print("圖書(shū)借閱成功~") break else: print("借閱者的名字不能為空, 請(qǐng)重新輸入") else: print("未查詢到相關(guān)書(shū)籍信息~") num = input("繼續(xù)借閱請(qǐng)輸入1, 回車退回主菜單") if num == "1": self.borrow_book() def back_book(self): """ [7]: 歸還圖書(shū) :return: """ print("****************歸還圖書(shū)****************") book_id = input("請(qǐng)輸入需要?dú)w還的圖書(shū)ID:") result = db.query("select * from books where id={};".format(book_id), one=True) if result: if result["status"] == "在庫(kù)": print("該書(shū)是在庫(kù)狀態(tài),請(qǐng)確認(rèn)圖書(shū)編號(hào)是否正確!") else: db.update("update books set status='在庫(kù)' where id={};".format(book_id)) db.update("update books set borrower='' where id={};".format(book_id)) print("書(shū)籍歸還成功~") else: print("未查詢到相關(guān)書(shū)籍信息~") num = input("繼續(xù)歸還書(shū)籍請(qǐng)輸入1, 回車退回主菜單") if num == "1": self.borrow_book() def quit(self): """ [8]: 退出 :return: """ print("****************退出****************") db.close() def main(self): """ 程序運(yùn)行的流程控制 :return: """ print("---------------歡迎進(jìn)入圖書(shū)管理系統(tǒng)----------------") while True: self.print_menu() num = input("請(qǐng)輸入選項(xiàng):") if num == "1": self.add_book() elif num == "2": self.update_book() elif num == "3": self.delete_book() elif num == "4": self.query_book() elif num == "5": self.book_list() elif num == "6": self.borrow_book() elif num == "7": self.back_book() elif num == "8": self.quit() break else: print("您的輸入有誤~ 請(qǐng)按照菜單提示輸入,謝謝!") if __name__ == "__main__": book = BookManage() book.main()
到此這篇關(guān)于教你用python實(shí)現(xiàn)一個(gè)無(wú)界面的小型圖書(shū)管理系統(tǒng)的文章就介紹到這了,更多相關(guān)Python實(shí)現(xiàn)圖書(shū)管理系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python編寫(xiě)圖書(shū)管理系統(tǒng)
- Python實(shí)現(xiàn)簡(jiǎn)易的圖書(shū)管理系統(tǒng)
- Python實(shí)現(xiàn)圖書(shū)借閱管理系統(tǒng)
- Python實(shí)現(xiàn)圖書(shū)管理系統(tǒng)設(shè)計(jì)
- 基于python實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- python代碼實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- python實(shí)現(xiàn)簡(jiǎn)單圖書(shū)管理系統(tǒng)
- Python簡(jiǎn)易版圖書(shū)管理系統(tǒng)
- wxpython實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- python圖書(shū)管理系統(tǒng)
- python實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- python實(shí)現(xiàn)簡(jiǎn)易圖書(shū)管理系統(tǒng)
相關(guān)文章
Django使用rest_framework寫(xiě)出API
這篇文章主要介紹了Django使用rest_framework寫(xiě)出API,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05使用OpenCV對(duì)運(yùn)動(dòng)員的姿勢(shì)進(jìn)行檢測(cè)功能實(shí)現(xiàn)
2022年奧林匹克運(yùn)動(dòng)會(huì)如期舉行,以不正確的方式進(jìn)行運(yùn)動(dòng)風(fēng)險(xiǎn)在增加,人體姿勢(shì)估計(jì)是計(jì)算機(jī)視覺(jué)領(lǐng)域的重要問(wèn)題,接下來(lái)通過(guò)本文給大家介紹下使用OpenCV對(duì)運(yùn)動(dòng)員的姿勢(shì)進(jìn)行檢測(cè)功能,感興趣的朋友一起看看吧2022-02-02

python爬蟲(chóng)今日熱榜數(shù)據(jù)到txt文件的源碼

利用Pytorch實(shí)現(xiàn)ResNet網(wǎng)絡(luò)構(gòu)建及模型訓(xùn)練