帮我写一个器件管理软件 有出库和入库功能 并且可以查询到历史记录 程序
时间: 2024-04-06 08:29:37 浏览: 46
cadence软件的菜单功能介绍
4星 · 用户满意度95%
好的,下面是一个简单的器件管理软件程序的示例,供您参考。
```python
import sqlite3
import datetime
def create_table():
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS inventory
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
model TEXT NOT NULL,
quantity INTEGER NOT NULL,
in_time TEXT NOT NULL,
out_time TEXT);''')
conn.commit()
conn.close()
def add_item(name, model, quantity):
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
in_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
c.execute("INSERT INTO inventory (name, model, quantity, in_time) VALUES (?, ?, ?, ?)", (name, model, quantity, in_time))
conn.commit()
conn.close()
def remove_item(name, model, quantity):
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
out_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
c.execute("SELECT quantity FROM inventory WHERE name=? AND model=?", (name, model))
result = c.fetchone()
if result and result[0] >= quantity:
c.execute("UPDATE inventory SET quantity=quantity-?, out_time=? WHERE name=? AND model=?", (quantity, out_time, name, model))
conn.commit()
conn.close()
return True
else:
conn.close()
return False
def search_item(name, model):
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
c.execute("SELECT * FROM inventory WHERE name=? AND model=?", (name, model))
result = c.fetchall()
conn.close()
return result
create_table()
while True:
print("Enter 1 to add item, 2 to remove item, 3 to search item, or 4 to exit:")
choice = input()
if choice == '1':
name = input("Enter item name: ")
model = input("Enter item model: ")
quantity = int(input("Enter quantity: "))
add_item(name, model, quantity)
elif choice == '2':
name = input("Enter item name: ")
model = input("Enter item model: ")
quantity = int(input("Enter quantity: "))
if remove_item(name, model, quantity):
print("Item removed successfully.")
else:
print("Item not found or quantity not enough.")
elif choice == '3':
name = input("Enter item name: ")
model = input("Enter item model: ")
result = search_item(name, model)
if result:
print("History:")
for row in result:
print("ID: {}, Name: {}, Model: {}, Quantity: {}, In Time: {}, Out Time: {}".format(row[0], row[1], row[2], row[3], row[4], row[5]))
else:
print("Item not found.")
elif choice == '4':
break
else:
print("Invalid choice.")
```
这个程序使用了 SQLite 数据库来存储器件信息,包括名称、型号、数量、入库时间、出库时间等。程序提供了添加、删除和查询器件信息的功能。
在运行程序前,请确保已经安装了 SQLite 数据库,并将程序保存在与数据库同一目录下。运行程序后,按照提示输入相应的编号即可使用相应的功能。
该程序仅供参考,实际应用中还需要根据具体需求进行修改和完善。
阅读全文