欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、需求了解

功能模塊

在這里插入圖片描述

圖書信息

在這里插入圖片描述

二、環(huán)境準備

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

參考文章:

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

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

創(chuàng)建數(shù)據(jù)庫表

  • 創(chuàng)建數(shù)據(jù)庫

CREATE DATABASE bookmanage;

  • 使用數(shù)據(jù)庫

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(‘在庫', ‘出借') default ‘在庫',
borrower varchar(20) default “”
);

  • 插入數(shù)據(jù)

insert into books(name, position) value (‘python從入門到放棄', ‘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;

三、代碼實現(xiàn)

引入pymysql模塊

  • 安裝pymysql

命令:pip install pymysql

  • 封裝操作數(shù)據(jù)庫模塊
# -*- 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ù)庫
    """
    def __init__(self, host, user, pwd, database=None, port=3306):
        """
        初始化數(shù)據(jù)庫鏈接
        :param host: 主機地址
        :param user: 用戶名
        :param pwd: 密碼
        :param database: 數(shù)據(jù)庫名稱,默認為空
        :param port: 端口號,默認3306
        """
        self.conn = pymysql.connect(
            host=host,
            user=user,
            password=pwd,
            database=database,
            port=port,
            cursorclass=pymysql.cursors.DictCursor
        )
        # 創(chuàng)建一個游標(biāo)對象
        self.cur = self.conn.cursor()

    def update(self, sql):
        """
        進行增刪改操作
        :param sql: 需要執(zhí)行的SQL
        :return:
        """
        # 執(zhí)行SQL
        result = self.cur.execute(sql)
        # 提交事務(wù)
        self.conn.commit()
        return result

    def query(self, sql, one=False):
        """
        進行查詢操作
        :param one: 判斷是要返回所有查詢數(shù)據(jù)還是第一條,默認是所有
        :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):
        """
        斷開游標(biāo),關(guān)閉數(shù)據(jù)庫連接
        :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從入門到放棄', 'A-1-1');")

圖案管理系統(tǒng)后臺實現(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:
    """
    圖書管理系統(tǒng)
    """

    @staticmethod
    def print_menu():
        """
        菜單打印
        :return:
        """
        print("---------------------菜單-------------------------")
        print("[1]: 添加圖書")
        print("[2]: 修改圖書")
        print("[3]: 刪除圖書")
        print("[4]: 查詢圖書")
        print("[5]: 圖書列表")
        print("[6]: 出借圖書")
        print("[7]: 歸還圖書")
        print("[8]: 退出")

    def add_book(self):
        """
        [1]: 添加圖書
        :return:
        """
        print("****************添加圖書****************")
        name = input("請輸入書名:")
        position = input("請輸入圖書位置:")
        if name and position:
            db.update("insert into books(name, position) value ('{}', '{}');".format(name, position))
            print("圖書添加成功")
        else:
            print("書名或者圖書位置不能為空,請重新輸入!")

        num = input("繼續(xù)添加請輸入1, 回車退回主菜單")
        if num == "1":
            self.add_book()

    def update_book(self):
        """
        [2]: 修改圖書
        :return:
        """
        print("****************修改圖書****************")
        book_id = input("請輸入需要修改的圖書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("重新輸入書名,不修改輸入回車:") 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("您輸入的圖書ID不存在,請重新輸入~")

        num = input("繼續(xù)修改請輸入1, 回車退回主菜單")
        if num == "1":
            self.update_book()

    def delete_book(self):
        """
        [3]: 刪除圖書
        :return:
        """
        print("****************刪除圖書****************")
        book_id = input("請輸入需要修改的圖書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("確定需要刪除這本書嗎?確認請按1,取消請按2:")
            if confirm_num == "1":
                db.update("delete from books where id={};".format(book_id))
                print("刪除成功")
            else:
                print("已確認不刪除該書籍~")
        else:
            print("系統(tǒng)中未找到該書籍!")

        num = input("繼續(xù)刪除請輸入1, 回車退回主菜單")
        if num == "1":
            self.delete_book()

    def query_book(self):
        """
        [4]: 查詢圖書
        :return:
        """
        print("****************查詢圖書****************")
        name = input("請輸入您要查詢的圖書名稱(模糊匹配):")
        if name:
            result = db.query("select * from books where name like '%{}%';".format(name))
            if result:
                print("當(dāng)前查詢到如下書籍信息:{}".format(result))
            else:
                print("未查詢到相關(guān)書籍信息~")
        else:
            print("書名不能為空!")

        num = input("繼續(xù)查詢請輸入1, 回車退回主菜單")
        if num == "1":
            self.query_book()

    def book_list(self):
        """
        [5]: 圖書列表
        :return:
        """
        print("****************圖書列表****************")
        result = db.query("select * from books;")
        for i in result:
            print("編號:{}, 書籍名:{}, 位置:{}, 狀態(tài):{}, 借閱人:{}".format(i["id"], i["name"], i["position"], i["status"],
                                                               i["borrower"]))

    def borrow_book(self):
        """
        [6]: 出借圖書
        :return:
        """
        print("****************出借圖書****************")
        book_id = input("請輸入需要借閱的圖書ID:")
        result = db.query("select * from books where id={};".format(book_id), one=True)
        if result:
            if result["status"] == "出借":
                print("抱歉,該書已經(jīng)借出!")
            else:
                while True:
                    borrower = input("請輸入借閱者的名字:")
                    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("圖書借閱成功~")
                        break
                    else:
                        print("借閱者的名字不能為空, 請重新輸入")
        else:
            print("未查詢到相關(guān)書籍信息~")

        num = input("繼續(xù)借閱請輸入1, 回車退回主菜單")
        if num == "1":
            self.borrow_book()

    def back_book(self):
        """
        [7]: 歸還圖書
        :return:
        """
        print("****************歸還圖書****************")
        book_id = input("請輸入需要歸還的圖書ID:")
        result = db.query("select * from books where id={};".format(book_id), one=True)
        if result:
            if result["status"] == "在庫":
                print("該書是在庫狀態(tài),請確認圖書編號是否正確!")
            else:
                db.update("update books set status='在庫' where id={};".format(book_id))
                db.update("update books set borrower='' where id={};".format(book_id))
                print("書籍歸還成功~")
        else:
            print("未查詢到相關(guān)書籍信息~")

        num = input("繼續(xù)歸還書籍請輸入1, 回車退回主菜單")
        if num == "1":
            self.borrow_book()

    def quit(self):
        """
        [8]: 退出
        :return:
        """
        print("****************退出****************")
        db.close()

    def main(self):
        """
        程序運行的流程控制
        :return:
        """
        print("---------------歡迎進入圖書管理系統(tǒng)----------------")
        while True:
            self.print_menu()
            num = input("請輸入選項:")
            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("您的輸入有誤~ 請按照菜單提示輸入,謝謝!")


if __name__ == "__main__":
    book = BookManage()
    book.main()

到此這篇關(guān)于教你用python實現(xiàn)一個無界面的小型圖書管理系統(tǒng)的文章就介紹到這了,更多相關(guān)Python實現(xiàn)圖書管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

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

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

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

    python pandas 如何替換某列的一個值

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

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

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