代码生成一个图书管理系统
时间: 2023-09-08 20:07:32 浏览: 106
好的,这里是一个简单的图书管理系统的代码示例,使用Python和SQLite数据库:
```python
import sqlite3
# 连接数据库
conn = sqlite3.connect('library.db')
c = conn.cursor()
# 创建图书表
c.execute('''CREATE TABLE IF NOT EXISTS books
(id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
author TEXT,
year INTEGER,
isbn TEXT)''')
# 添加图书
def add_book(title, author, year, isbn):
c.execute("INSERT INTO books (title, author, year, isbn) VALUES (?, ?, ?, ?)",
(title, author, year, isbn))
conn.commit()
# 更新图书
def update_book(id, title, author, year, isbn):
c.execute("UPDATE books SET title=?, author=?, year=?, isbn=? WHERE id=?",
(title, author, year, isbn, id))
conn.commit()
# 删除图书
def delete_book(id):
c.execute("DELETE FROM books WHERE id=?", (id,))
conn.commit()
# 搜索图书
def search_book(title="", author="", year="", isbn=""):
c.execute("SELECT * FROM books WHERE title=? OR author=? OR year=? OR isbn=?",
(title, author, year, isbn))
return c.fetchall()
# 显示所有图书
def view_all_books():
c.execute("SELECT * FROM books")
return c.fetchall()
# 关闭数据库连接
def close_db():
conn.close()
```
这个简单的图书管理系统包含了添加图书、更新图书、删除图书、搜索图书、显示所有图书等功能。你可以根据自己的需求进行修改和调整。
阅读全文