帮我写下这个代码
时间: 2023-08-06 21:24:08 浏览: 33
好的,根据您的需求,我为您编写了一个简单的库存管理系统的代码,使用Python语言和SQLite数据库。请注意,这只是一个简单的示例代码,您可以根据自己的实际需求进行修改和完善。
首先,需要先安装Python和SQLite3模块。然后,您可以按照以下代码进行编写:
```python
import sqlite3
# 连接数据库
conn = sqlite3.connect('inventory.db')
# 创建数据表
conn.execute('''CREATE TABLE IF NOT EXISTS inventory
(id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
quantity INTEGER NOT NULL,
price REAL NOT NULL)''')
# 添加物品
def add_item(name, quantity, price):
conn.execute("INSERT INTO inventory (name, quantity, price) VALUES (?, ?, ?)", (name, quantity, price))
conn.commit()
print("物品添加成功!")
# 更新物品
def update_item(id, name, quantity, price):
conn.execute("UPDATE inventory SET name = ?, quantity = ?, price = ? WHERE id = ?", (name, quantity, price, id))
conn.commit()
print("物品更新成功!")
# 删除物品
def delete_item(id):
conn.execute("DELETE FROM inventory WHERE id = ?", (id,))
conn.commit()
print("物品删除成功!")
# 显示物品列表
def show_items():
cursor = conn.execute("SELECT id, name, quantity, price FROM inventory")
for row in cursor:
print("ID = ", row[0])
print("名称 = ", row[1])
print("数量 = ", row[2])
print("价格 = ", row[3], "\n")
# 关闭数据库连接
def close_connection():
conn.close()
# 主程序
if __name__ == '__main__':
while True:
print("欢迎使用库存管理系统!")
print("1. 添加物品")
print("2. 更新物品")
print("3. 删除物品")
print("4. 显示物品列表")
print("5. 退出程序")
choice = input("请输入选项:")
if choice == '1':
name = input("请输入名称:")
quantity = int(input("请输入数量:"))
price = float(input("请输入价格:"))
add_item(name, quantity, price)
elif choice == '2':
id = int(input("请输入要更新的物品ID:"))
name = input("请输入新名称:")
quantity = int(input("请输入新数量:"))
price = float(input("请输入新价格:"))
update_item(id, name, quantity, price)
elif choice == '3':
id = int(input("请输入要删除的物品ID:"))
delete_item(id)
elif choice == '4':
print("物品列表如下:")
show_items()
elif choice == '5':
print("感谢使用库存管理系统!")
close_connection()
break
else:
print("无效选项,请重新输入!")
```
这段代码实现了库存管理系统的基本功能,包括添加物品、更新物品、删除物品和显示物品列表等。您可以根据自己的需求进行修改和改进。