亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

教你用python實(shí)現(xiàn)一個(gè)無(wú)界面的小型圖書(shū)管理系統(tǒng)

 更新時(shí)間:2021年05月21日 09:03:49   作者:florachy  
今天帶大家學(xué)習(xí)怎么用python實(shí)現(xiàn)一個(gè)無(wú)界面的小型圖書(shū)管理系統(tǒng),文中有非常詳細(xì)的圖文解說(shuō)及代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下

一、需求了解

功能模塊

在這里插入圖片描述

圖書(shū)信息

在這里插入圖片描述

二、環(huán)境準(zhǔn)備

安裝mysql數(shù)據(jù)庫(kù)

參考文章:

MySQL數(shù)據(jù)庫(kù)壓縮版本安裝與配置

MySQL msi版本下載安裝圖文教程

創(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python中的遞歸函數(shù)使用詳解

    Python中的遞歸函數(shù)使用詳解

    這篇文章主要介紹了Python中的遞歸函數(shù)使用詳解,遞歸函數(shù)是指某個(gè)函數(shù)調(diào)用自己或者調(diào)用其他函數(shù)后再次調(diào)用自己,由于不能無(wú)限嵌套調(diào)用,所以某個(gè)遞歸函數(shù)一定存在至少兩個(gè)分支,一個(gè)是退出嵌套,不再直接或者間接調(diào)用自己;另外一個(gè)則是繼續(xù)嵌套,需要的朋友可以參考下
    2023-12-12
  • python爬蟲(chóng)今日熱榜數(shù)據(jù)到txt文件的源碼

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

    這篇文章主要介紹了python爬蟲(chóng)今日熱榜數(shù)據(jù)到txt文件的源碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • python pandas 如何替換某列的一個(gè)值

    python pandas 如何替換某列的一個(gè)值

    python pandas 如何替換某列的一個(gè)值?今天小編就為大家分享一篇python pandas 實(shí)現(xiàn)替換某列的一個(gè)值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助
    2018-06-06
  • 利用Pytorch實(shí)現(xiàn)ResNet網(wǎng)絡(luò)構(gòu)建及模型訓(xùn)練

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

    這篇文章主要為大家介紹了利用Pytorch實(shí)現(xiàn)ResNet網(wǎng)絡(luò)構(gòu)建及模型訓(xùn)練詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • 最新評(píng)論