人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

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

發(fā)布日期:2022-05-17 14:25 | 文章來源:CSDN

一、需求了解

功能模塊

圖書信息

二、環(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: 端口號(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):
  """
  斷開游標(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)后臺(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:
 """
 圖書管理系統(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("請(qǐng)輸入書名:")
  position = input("請(qǐng)輸入圖書位置:")
  if name and position:
db.update("insert into books(name, position) value ('{}', '{}');".format(name, position))
print("圖書添加成功")
  else:
print("書名或者圖書位置不能為空,請(qǐng)重新輸入!")
  num = input("繼續(xù)添加請(qǐng)輸入1, 回車退回主菜單")
  if num == "1":
self.add_book()
 def update_book(self):
  """
  [2]: 修改圖書
  :return:
  """
  print("****************修改圖書****************")
  book_id = input("請(qǐng)輸入需要修改的圖書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不存在,請(qǐng)重新輸入~")
  num = input("繼續(xù)修改請(qǐng)輸入1, 回車退回主菜單")
  if num == "1":
self.update_book()
 def delete_book(self):
  """
  [3]: 刪除圖書
  :return:
  """
  print("****************刪除圖書****************")
  book_id = input("請(qǐng)輸入需要修改的圖書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除這本書嗎?確認(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)不刪除該書籍~")
  else:
print("系統(tǒng)中未找到該書籍!")
  num = input("繼續(xù)刪除請(qǐng)輸入1, 回車退回主菜單")
  if num == "1":
self.delete_book()
 def query_book(self):
  """
  [4]: 查詢圖書
  :return:
  """
  print("****************查詢圖書****************")
  name = input("請(qǐng)輸入您要查詢的圖書名稱(模糊匹配):")
  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ù)查詢請(qǐng)輸入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("編號(hào):{}, 書籍名:{}, 位置:{}, 狀態(tài):{}, 借閱人:{}".format(i["id"], i["name"], i["position"], i["status"],
 i["borrower"]))
 def borrow_book(self):
  """
  [6]: 出借圖書
  :return:
  """
  print("****************出借圖書****************")
  book_id = input("請(qǐng)輸入需要借閱的圖書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("請(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("圖書借閱成功~")break
  else:print("借閱者的名字不能為空, 請(qǐng)重新輸入")
  else:
print("未查詢到相關(guān)書籍信息~")
  num = input("繼續(xù)借閱請(qǐng)輸入1, 回車退回主菜單")
  if num == "1":
self.borrow_book()
 def back_book(self):
  """
  [7]: 歸還圖書
  :return:
  """
  print("****************歸還圖書****************")
  book_id = input("請(qǐng)輸入需要?dú)w還的圖書ID:")
  result = db.query("select * from books where id={};".format(book_id), one=True)
  if result:
if result["status"] == "在庫":
 print("該書是在庫狀態(tài),請(qǐng)確認(rèn)圖書編號(hào)是否正確!")
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ù)歸還書籍請(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)入圖書管理系統(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è)無界面的小型圖書管理系統(tǒng)的文章就介紹到這了,更多相關(guān)Python實(shí)現(xiàn)圖書管理系統(tǒng)內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

美國快速服務(wù)器

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部