python用模板mysql-connector设计汽车数据库,有1.不要设计类,连接数据库 2.设计一个窗体,数据库建立admin和sales表 3.有添加,删除,修改,查询等功能 4.添加有time,buyer,brand,model,price的元素 5.查询可以salea表的内容有time,buyer,brand,model,price的元素 6.添加,删除,修改,查询都有按钮
时间: 2023-12-06 08:03:49 浏览: 124
好的,我会根据你的要求,再次修改代码,并添加缺失的部分,以下是完整的代码片段:
```
import mysql.connector
import datetime
import tkinter as tk
from tkinter import messagebox
# 连接到MySQL数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
mycursor = mydb.cursor()
# 创建窗体
root = tk.Tk()
root.title("汽车数据库")
# 创建标签
admin_table = tk.Label(root, text="admin表")
admin_table.pack()
sales_table = tk.Label(root, text="sales表")
sales_table.pack()
# 添加元素到sales表
def add_record():
now = datetime.datetime.now()
buyer = buyer_entry.get()
brand = brand_entry.get()
model = model_entry.get()
price = price_entry.get()
sql = "INSERT INTO sales (time, buyer, brand, model, price) VALUES (%s, %s, %s, %s, %s)"
val = (now, buyer, brand, model, price)
mycursor.execute(sql, val)
mydb.commit()
messagebox.showinfo("添加记录", "记录添加成功。")
# 删除sales表的一条记录
def delete_record():
id = id_entry.get()
sql = "DELETE FROM sales WHERE id = %s"
val = (id,)
mycursor.execute(sql, val)
mydb.commit()
messagebox.showinfo("删除记录", "记录删除成功。")
# 修改sales表的一条记录
def update_record():
id = id_entry.get()
buyer = buyer_entry.get()
brand = brand_entry.get()
model = model_entry.get()
price = price_entry.get()
sql = "UPDATE sales SET buyer = %s, brand = %s, model = %s, price = %s WHERE id = %s"
val = (buyer, brand, model, price, id)
mycursor.execute(sql, val)
mydb.commit()
messagebox.showinfo("修改记录", "记录修改成功。")
# 查询sales表的内容
def search_record():
mycursor.execute("SELECT time, buyer, brand, model, price FROM sales")
myresult = mycursor.fetchall()
result_text = ""
for x in myresult:
result_text += "购买时间:{},购买人:{},品牌:{},型号:{},价格:{}\n".format(x[0], x[1], x[2], x[3], x[4])
messagebox.showinfo("查询结果", result_text)
# 创建输入框和按钮
id_label = tk.Label(root, text="ID:")
id_label.pack()
id_entry = tk.Entry(root)
id_entry.pack()
buyer_label = tk.Label(root, text="购买人:")
buyer_label.pack()
buyer_entry = tk.Entry(root)
buyer_entry.pack()
brand_label = tk.Label(root, text="品牌:")
brand_label.pack()
brand_entry = tk.Entry(root)
brand_entry.pack()
model_label = tk.Label(root, text="型号:")
model_label.pack()
model_entry = tk.Entry(root)
model_entry.pack()
price_label = tk.Label(root, text="价格:")
price_label.pack()
price_entry = tk.Entry(root)
price_entry.pack()
add_button = tk.Button(root, text="添加", command=add_record)
add_button.pack()
delete_button = tk.Button(root, text="删除", command=delete_record)
delete_button.pack()
update_button = tk.Button(root, text="修改", command=update_record)
update_button.pack()
search_button = tk.Button(root, text="查询", command=search_record)
search_button.pack()
root.mainloop()
```
你需要将`yourusername`,`yourpassword`和`yourdatabase`替换为你自己的MySQL用户名、密码和数据库名称。
希望这个代码可以帮助你完成你的任务!