根据以下给的数据库register表字段,用PyQt5+MySQL实现增删改查,代码示例,register表字段如下: `id` int `register_type` varchar(255) `tooth_number` varchar(255) `hospital_name` varchar(255) `doctor_name` varchar(255) `patient_name` varchar(255) `color_name` varchar(255) `category_name` varchar(255) `price` varchar(255) `tooth_a` varchar(255) `tooth_b` varchar(255) `tooth_c` varchar(255) `tooth_d` varchar(255) `number` `amount_of_money` varchar(255) `delivery_date` varchar(255) `register_date` varchar(255) `remark` varchar(255) `total_amount` varchar(255)
时间: 2023-03-31 12:01:31 浏览: 120
可以的,以下是示例代码:
# 导入PyQt5和MySQL库
from PyQt5.QtWidgets import *
import mysql.connector
# 连接MySQL数据库
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="database_name"
)
# 创建游标对象
mycursor = mydb.cursor()
# 实现增加数据的函数
def add_data():
# 获取用户输入的数据
id = id_input.text()
register_type = register_type_input.text()
tooth_number = tooth_number_input.text()
hospital_name = hospital_name_input.text()
doctor_name = doctor_name_input.text()
patient_name = patient_name_input.text()
color_name = color_name_input.text()
category_name = category_name_input.text()
price = price_input.text()
tooth_a = tooth_a_input.text()
tooth_b = tooth_b_input.text()
tooth_c = tooth_c_input.text()
tooth_d = tooth_d_input.text()
number = number_input.text()
amount_of_money = amount_of_money_input.text()
delivery_date = delivery_date_input.text()
register_date = register_date_input.text()
remark = remark_input.text()
total_amount = total_amount_input.text()
# 执行SQL语句,将数据插入到register表中
sql = "INSERT INTO register (id, register_type, tooth_number, hospital_name, doctor_name, patient_name, color_name, category_name, price, tooth_a, tooth_b, tooth_c, tooth_d, number, amount_of_money, delivery_date, register_date, remark, total_amount) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = (id, register_type, tooth_number, hospital_name, doctor_name, patient_name, color_name, category_name, price, tooth_a, tooth_b, tooth_c, tooth_d, number, amount_of_money, delivery_date, register_date, remark, total_amount)
mycursor.execute(sql, val)
mydb.commit()
# 提示用户添加成功
QMessageBox.information(window, "提示", "添加成功!")
# 实现删除数据的函数
def delete_data():
# 获取用户输入的id
id = id_input.text()
# 执行SQL语句,删除register表中指定id的数据
sql = "DELETE FROM register WHERE id = %s"
val = (id,)
mycursor.execute(sql, val)
mydb.commit()
# 提示用户删除成功
QMessageBox.information(window, "提示", "删除成功!")
# 实现修改数据的函数
def update_data():
# 获取用户输入的数据
id = id_input.text()
register_type = register_type_input.text()
tooth_number = tooth_number_input.text()
hospital_name = hospital_name_input.text()
doctor_name = doctor_name_input.text()
patient_name = patient_name_input.text()
color_name = color_name_input.text()
category_name = category_name_input.text()
price = price_input.text()
tooth_a = tooth_a_input.text()
tooth_b = tooth_b_input.text()
tooth_c = tooth_c_input.text()
tooth_d = tooth_d_input.text()
number = number_input.text()
amount_of_money = amount_of_money_input.text()
delivery_date = delivery_date_input.text()
register_date = register_date_input.text()
remark = remark_input.text()
total_amount = total_amount_input.text()
# 执行SQL语句,更新register表中指定id的数据
sql = "UPDATE register SET register_type = %s, tooth_number = %s, hospital_name = %s, doctor_name = %s, patient_name = %s, color_name = %s, category_name = %s, price = %s, tooth_a = %s, tooth_b = %s, tooth_c = %s, tooth_d = %s, number = %s, amount_of_money = %s, delivery_date = %s, register_date = %s, remark = %s, total_amount = %s WHERE id = %s"
val = (register_type, tooth_number, hospital_name, doctor_name, patient_name, color_name, category_name, price, tooth_a, tooth_b, tooth_c, tooth_d, number, amount_of_money, delivery_date, register_date, remark, total_amount, id)
mycursor.execute(sql, val)
mydb.commit()
# 提示用户修改成功
QMessageBox.information(window, "提示", "修改成功!")
# 实现查询数据的函数
def search_data():
# 获取用户输入的id
id = id_input.text()
# 执行SQL语句,查询register表中指定id的数据
sql = "SELECT * FROM register WHERE id = %s"
val = (id,)
mycursor.execute(sql, val)
result = mycursor.fetchone()
# 将查询结果显示在界面上
register_type_input.setText(result[1])
tooth_number_input.setText(result[2])
hospital_name_input.setText(result[3])
doctor_name_input.setText(result[4])
patient_name_input.setText(result[5])
color_name_input.setText(result[6])
category_name_input.setText(result[7])
price_input.setText(result[8])
tooth_a_input.setText(result[9])
tooth_b_input.setText(result[10])
tooth_c_input.setText(result[11])
tooth_d_input.setText(result[12])
number_input.setText(result[13])
amount_of_money_input.setText(result[14])
delivery_date_input.setText(result[15])
register_date_input.setText(result[16])
remark_input.setText(result[17])
total_amount_input.setText(result[18])
# 创建应用程序
app = QApplication([])
window = QWidget()
window.setWindowTitle("PyQt5 MySQL实现增删改查")
window.resize(800, 600)
# 创建输入框和标签
id_label = QLabel("id:")
id_input = QLineEdit()
register_type_label = QLabel("register_type:")
register_type_input = QLineEdit()
tooth_number_label = QLabel("tooth_number:")
tooth_number_input = QLineEdit()
hospital_name_label = QLabel("hospital_name:")
hospital_name_input = QLineEdit()
doctor_name_label = QLabel("doctor_name:")
doctor_name_input = QLineEdit()
patient_name_label = QLabel("patient_name:")
patient_name_input = QLineEdit()
color_name_label = QLabel("color_name:")
color_name_input = QLineEdit()
category_name_label = QLabel("category_name:")
category_name_input = QLineEdit()
price_label = QLabel("price:")
price_input = QLineEdit()
tooth_a_label = QLabel("tooth_a:")
tooth_a_input = QLineEdit()
tooth_b_label = QLabel("tooth_b:")
tooth_b_input = QLineEdit()
tooth_c_label = QLabel("tooth_c:")
tooth_c_input = QLineEdit()
tooth_d_label = QLabel("tooth_d:")
tooth_d_input = QLineEdit()
number_label = QLabel("number:")
number_input = QLineEdit()
amount_of_money_label = QLabel("amount_of_money:")
amount_of_money_input = QLineEdit()
delivery_date_label = QLabel("delivery_date:")
delivery_date_input = QLineEdit()
register_date_label = QLabel("register_date:")
register_date_input = QLineEdit()
remark_label = QLabel("remark:")
remark_input = QLineEdit()
total_amount_label = QLabel("total_amount:")
total_amount_input = QLineEdit()
# 创建按钮
add_button = QPushButton("添加")
delete_button = QPushButton("删除")
update_button = QPushButton("修改")
search_button = QPushButton("查询")
# 将输入框和标签、按钮添加到布局中
layout = QGridLayout()
layout.addWidget(id_label, , )
layout.addWidget(id_input, , 1)
layout.addWidget(register_type_label, 1, )
layout.addWidget(register_type_input, 1, 1)
layout.addWidget(tooth_number_label, 2, )
layout.addWidget(tooth_number_input, 2, 1)
layout.addWidget(hospital_name_label, 3, )
layout.addWidget(hospital_name_input, 3, 1)
layout.addWidget(doctor_name_label, 4, )
layout.addWidget(doctor_name_input, 4, 1)
layout.addWidget(patient_name_label, 5, )
layout.addWidget(patient_name_input, 5, 1)
layout.addWidget(color_name_label, 6, )
layout.addWidget(color_name_input, 6, 1)
layout.addWidget(category_name_label, 7, )
layout.addWidget(category_name_input, 7, 1)
layout.addWidget(price_label, 8, )
layout.addWidget(price_input, 8, 1)
layout.addWidget(tooth_a_label, 9, )
layout.addWidget(tooth_a_input, 9, 1)
layout.addWidget(tooth_b_label, 10, )
layout.addWidget(tooth_b_input, 10, 1)
layout.addWidget(tooth_c_label, 11, )
layout.addWidget(tooth_c_input, 11, 1)
layout.addWidget(tooth_d_label, 12, )
layout.addWidget(tooth_d_input, 12, 1)
layout.addWidget(number_label, 13, )
layout.addWidget(number_input, 13, 1)
layout.addWidget(amount_of_money_label, 14, )
layout.addWidget(amount_of_money_input, 14, 1)
layout.addWidget(delivery_date_label, 15, )
layout.addWidget(delivery_date_input, 15, 1)
layout.addWidget(register_date_label, 16, )
layout.addWidget(register_date_input, 16, 1)
layout.addWidget(remark_label, 17, )
layout.addWidget(remark_input, 17, 1)
layout.addWidget(total_amount_label, 18, )
layout.addWidget(total_amount_input, 18, 1)
layout.addWidget(add_button, 19, )
layout.addWidget(delete_button, 19, 1)
layout.addWidget(update_button, 20, )
layout.addWidget(search_button, 20, 1)
# 将布局添加到窗口中
window.setLayout(layout)
# 绑定按钮的点击事件
add_button.clicked.connect(add_data)
delete_button.clicked.connect(delete_data)
update_button.clicked.connect(update_data)
search_button.clicked.connect(search_data)
# 显示窗口
window.show()
app.exec_()
阅读全文