婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 教你用python實(shí)現(xiàn)一個無界面的小型圖書管理系統(tǒng)

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

熱門標(biāo)簽:沈陽人工外呼系統(tǒng)價格 如何申請400電話費(fèi)用 池州外呼調(diào)研線路 外呼系統(tǒng)哪些好辦 武漢外呼系統(tǒng)平臺 沈陽防封電銷卡品牌 江西省地圖標(biāo)注 沈陽外呼系統(tǒng)呼叫系統(tǒng) 富錦商家地圖標(biāo)注

一、需求了解

功能模塊

圖書信息

二、環(huán)境準(zhǔ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;

三、代碼實(shí)現(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: 主機(jī)地址
        :param user: 用戶名
        :param pwd: 密碼
        :param database: 數(shù)據(jù)庫名稱,默認(rèn)為空
        :param port: 端口號,默認(rèn)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):
        """
        進(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):
        """
        斷開游標(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)后臺實(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:
    """
    圖書管理系統(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("確定需要刪除這本書嗎?確認(rèn)請按1,取消請按2:")
            if confirm_num == "1":
                db.update("delete from books where id={};".format(book_id))
                print("刪除成功")
            else:
                print("已確認(rèn)不刪除該書籍~")
        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("請輸入需要?dú)w還的圖書ID:")
        result = db.query("select * from books where id={};".format(book_id), one=True)
        if result:
            if result["status"] == "在庫":
                print("該書是在庫狀態(tài),請確認(rèn)圖書編號是否正確!")
            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):
        """
        程序運(yùn)行的流程控制
        :return:
        """
        print("---------------歡迎進(jìn)入圖書管理系統(tǒng)----------------")
        while True:
            self.print_menu()
            num = input("請輸入選項(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("您的輸入有誤~ 請按照菜單提示輸入,謝謝!")


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

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

您可能感興趣的文章:
  • Python函數(shù)實(shí)現(xiàn)學(xué)員管理系統(tǒng)
  • python面向?qū)ο蟀鎸W(xué)生信息管理系統(tǒng)
  • Python實(shí)現(xiàn)學(xué)生管理系統(tǒng)(面向?qū)ο蟀?
  • python學(xué)生信息管理系統(tǒng)實(shí)現(xiàn)代碼
  • 教你用Python實(shí)現(xiàn)簡易版學(xué)生信息管理系統(tǒng)(含源碼)
  • 基于Python實(shí)現(xiàn)的購物商城管理系統(tǒng)
  • 一篇文章教你用Python實(shí)現(xiàn)一個學(xué)生管理系統(tǒng)

標(biāo)簽:呂梁 常德 黑龍江 通遼 阿里 銅川 潛江 株洲

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《教你用python實(shí)現(xiàn)一個無界面的小型圖書管理系統(tǒng)》,本文關(guān)鍵詞  教,你用,python,實(shí)現(xiàn),一個,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《教你用python實(shí)現(xiàn)一個無界面的小型圖書管理系統(tǒng)》相關(guān)的同類信息!
  • 本頁收集關(guān)于教你用python實(shí)現(xiàn)一個無界面的小型圖書管理系統(tǒng)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 四会市| 兖州市| 三穗县| 乌鲁木齐县| 张家口市| 烟台市| 屯昌县| 金堂县| 崇义县| 辽宁省| 博兴县| 娱乐| 喀喇沁旗| 赤水市| 金门县| 象山县| 深泽县| 娄底市| 上饶市| 定陶县| 台中市| 遂宁市| 塔河县| 容城县| 宁南县| 灌云县| 新巴尔虎右旗| 丹阳市| 油尖旺区| 六枝特区| 会泽县| 应用必备| 花垣县| 香格里拉县| 九龙坡区| 改则县| 甘肃省| 来宾市| 五指山市| 江孜县| 德安县|