可以把这个代码写出来吗
时间: 2024-01-22 11:01:41 浏览: 27
好的,以下是一个基于Python和SQLite的简单自行车管理系统的代码示例,包含数据录入、存储、读写、修改、插入、删除和查询功能:
```python
import sqlite3
# 连接到数据库
conn = sqlite3.connect('bikes.db')
# 创建自行车表
conn.execute('''CREATE TABLE IF NOT EXISTS BIKES
(ID INT PRIMARY KEY NOT NULL,
BRAND TEXT NOT NULL,
MODEL TEXT NOT NULL,
COLOR TEXT NOT NULL,
PRICE REAL NOT NULL);''')
print("自行车管理系统\n")
while True:
print("请选择操作:\n")
print("1. 数据录入")
print("2. 数据查询")
print("3. 数据修改")
print("4. 数据插入")
print("5. 数据删除")
print("6. 退出系统\n")
choice = input()
# 数据录入
if choice == '1':
id = input("请输入自行车ID:")
brand = input("请输入自行车品牌:")
model = input("请输入自行车型号:")
color = input("请输入自行车颜色:")
price = input("请输入自行车价格:")
# 将数据插入到数据库中
conn.execute("INSERT INTO BIKES (ID,BRAND,MODEL,COLOR,PRICE) \
VALUES (?,?,?,?,?)", (id,brand,model,color,price))
conn.commit()
print("数据录入成功\n")
# 数据查询
elif choice == '2':
keyword = input("请输入查询关键字:")
cursor = conn.execute("SELECT * from BIKES WHERE BRAND LIKE ? OR MODEL LIKE ? OR COLOR LIKE ?",
('%' + keyword + '%', '%' + keyword + '%', '%' + keyword + '%'))
for row in cursor:
print("ID = ", row[0])
print("BRAND = ", row[1])
print("MODEL = ", row[2])
print("COLOR = ", row[3])
print("PRICE = ", row[4], "\n")
# 数据修改
elif choice == '3':
id = input("请输入要修改的自行车ID:")
brand = input("请输入自行车品牌:")
model = input("请输入自行车型号:")
color = input("请输入自行车颜色:")
price = input("请输入自行车价格:")
# 更新数据库中指定的数据
conn.execute("UPDATE BIKES SET BRAND = ?, MODEL = ?, COLOR = ?, PRICE = ? WHERE ID = ?",
(brand, model, color, price, id))
conn.commit()
print("数据修改成功\n")
# 数据插入
elif choice == '4':
id = input("请输入自行车ID:")
brand = input("请输入自行车品牌:")
model = input("请输入自行车型号:")
color = input("请输入自行车颜色:")
price = input("请输入自行车价格:")
# 将数据插入到数据库中
conn.execute("INSERT INTO BIKES (ID,BRAND,MODEL,COLOR,PRICE) \
VALUES (?,?,?,?,?)", (id,brand,model,color,price))
conn.commit()
print("数据插入成功\n")
# 数据删除
elif choice == '5':
id = input("请输入要删除的自行车ID:")
# 从数据库中删除指定的数据
conn.execute("DELETE from BIKES where ID = ?", (id,))
conn.commit()
print("数据删除成功\n")
# 退出系统
elif choice == '6':
print("感谢使用自行车管理系统,再见!")
break
else:
print("输入有误,请重新输入\n")
# 关闭数据库连接
conn.close()
```
需要注意的是,以上代码仅作为示例,可能存在不足之处。在实际使用中,需要根据具体需求进行优化和完善。