图书管理系统python实现
时间: 2023-09-01 07:07:28 浏览: 124
图书管理系统是一个常见的实践项目,可以用来练习Python编程和数据库设计。以下是一个简单的Python实现,使用SQLite作为数据库。
首先,我们需要安装SQLite3模块,可以使用以下命令:
```
pip install sqlite3
```
接下来,我们可以创建一个Python文件,并导入SQLite3模块:
```
import sqlite3
```
然后,我们需要创建一个数据库连接,并创建一个表来存储图书信息。以下是一个例子:
```
conn = sqlite3.connect('library.db')
c = conn.cursor()
c.execute('''CREATE TABLE books
(id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
author TEXT,
year INTEGER,
isbn TEXT)''')
conn.commit()
conn.close()
```
这个代码片段创建了一个名为"library.db"的数据库,并在其中创建了一个名为"books"的表,用于存储图书信息。
接下来,我们可以编写一些函数来实现图书管理系统的各种功能,例如添加图书、删除图书、更新图书信息、搜索图书等。
以下是一个例子:
```
def add_book(title, author, year, isbn):
conn = sqlite3.connect('library.db')
c = conn.cursor()
c.execute("INSERT INTO books (title, author, year, isbn) VALUES (?, ?, ?, ?)", (title, author, year, isbn))
conn.commit()
conn.close()
def delete_book(id):
conn = sqlite3.connect('library.db')
c = conn.cursor()
c.execute("DELETE FROM books WHERE id=?", (id,))
conn.commit()
conn.close()
def update_book(id, title, author, year, isbn):
conn = sqlite3.connect('library.db')
c = conn.cursor()
c.execute("UPDATE books SET title=?, author=?, year=?, isbn=? WHERE id=?", (title, author, year, isbn, id))
conn.commit()
conn.close()
def search_book(title="", author="", year="", isbn=""):
conn = sqlite3.connect('library.db')
c = conn.cursor()
c.execute("SELECT * FROM books WHERE title=? OR author=? OR year=? OR isbn=?", (title, author, year, isbn))
rows = c.fetchall()
conn.close()
return rows
```
这些函数可以通过图形界面或命令行界面来调用,并实现相应的功能。
最后,我们可以创建一个简单的图形界面来使用这些函数。以下是一个例子:
```
from tkinter import *
def view_command():
list1.delete(0, END)
for row in search_book():
list1.insert(END, row)
def search_command():
list1.delete(0, END)
for row in search_book(title_text.get(), author_text.get(), year_text.get(), isbn_text.get()):
list1.insert(END, row)
def add_command():
add_book(title_text.get(), author_text.get(), year_text.get(), isbn_text.get())
list1.delete(0, END)
list1.insert(END, (title_text.get(), author_text.get(), year_text.get(), isbn_text.get()))
def delete_command():
delete_book(selected_tuple[0])
def update_command():
update_book(selected_tuple[0], title_text.get(), author_text.get(), year_text.get(), isbn_text.get())
def get_selected_row(event):
global selected_tuple
index = list1.curselection()[0]
selected_tuple = list1.get(index)
e1.delete(0, END)
e1.insert(END, selected_tuple[1])
e2.delete(0, END)
e2.insert(END, selected_tuple[2])
e3.delete(0, END)
e3.insert(END, selected_tuple[3])
e4.delete(0, END)
e4.insert(END, selected_tuple[4])
window = Tk()
window.wm_title("Bookstore")
l1 = Label(window, text="Title")
l1.grid(row=0, column=0)
l2 = Label(window, text="Author")
l2.grid(row=0, column=2)
l3 = Label(window, text="Year")
l3.grid(row=1, column=0)
l4 = Label(window, text="ISBN")
l4.grid(row=1, column=2)
title_text = StringVar()
e1 = Entry(window, textvariable=title_text)
e1.grid(row=0, column=1)
author_text = StringVar()
e2 = Entry(window, textvariable=author_text)
e2.grid(row=0, column=3)
year_text = StringVar()
e3 = Entry(window, textvariable=year_text)
e3.grid(row=1, column=1)
isbn_text = StringVar()
e4 = Entry(window, textvariable=isbn_text)
e4.grid(row=1, column=3)
list1 = Listbox(window, height=6, width=35)
list1.grid(row=2, column=0, rowspan=6, columnspan=2)
sb1 = Scrollbar(window)
sb1.grid(row=2, column=2, rowspan=6)
list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)
list1.bind('<<ListboxSelect>>', get_selected_row)
b1 = Button(window, text="View all", width=12, command=view_command)
b1.grid(row=2, column=3)
b2 = Button(window, text="Search entry", width=12, command=search_command)
b2.grid(row=3, column=3)
b3 = Button(window, text="Add entry", width=12, command=add_command)
b3.grid(row=4, column=3)
b4 = Button(window, text="Update selected", width=12, command=update_command)
b4.grid(row=5, column=3)
b5 = Button(window, text="Delete selected", width=12, command=delete_command)
b5.grid(row=6, column=3)
b6 = Button(window, text="Close", width=12, command=window.destroy)
b6.grid(row=7, column=3)
window.mainloop()
```
这个代码片段创建了一个基本的GUI界面,包含一些按钮和文本框,用户可以使用这些按钮来进行图书管理系统的各种操作。
以上是一个简单的Python实现图书管理系统的示例代码,您可以根据自己的需求进行修改和扩展。
阅读全文