def show_query6_result(self): # 获取员工ID id, ok = QInputDialog.getText(self, '输入员工ID', '请输入员工ID:') if not ok: return # 查询数据 db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset') cursor = db.cursor() db_sql = """ SELECT a.user_id, b.`name`, b.phone, get_time, get_kg, recycle_time, recycle_kg, CONCAT( ROUND( ROUND( yield_rate, 4 ) * 100, 2 ), '%' ) yield_rate FROM em_salary a LEFT JOIN ( SELECT user_id, `name`, phone FROM `employee_table` ) b ON a.user_id = b.user_id ORDER BY get_time DESC """.format(id=id) cursor.execute(db_sql) result = cursor.fetchall() db.close() if len(result) == 0: QMessageBox.information(self, "提示", "今日无员工工资记录") return self.query6_window.table_widget.setRowCount(0) self.query6_window.table_widget.setColumnCount(len(result[0])) self.query6_window.table_widget.setHorizontalHeaderLabels( ['员工编号', '姓名', '电话', '领取时间', '重量', '交鸡皮时间', '鸡皮重量', '出成率'] ) for row_num, row_data in enumerate(result): self.query6_window.table_widget.insertRow(row_num) for col_num, col_data in enumerate(row_data): self.query6_window.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data))) self.query6_window.show()怎么改为输入的ID数据库没有就提示没有该员工ID,如果有,返回该ID数据
时间: 2023-12-22 07:05:28 浏览: 115
可以在查询数据之前,先执行一个查询语句,判断是否存在该员工ID,如果存在再执行查询数据的语句,否则提示不存在该员工ID。以下是修改后的代码:
```
def show_query6_result(self):
# 获取员工ID
id, ok = QInputDialog.getText(self, '输入员工ID', '请输入员工ID:')
if not ok:
return
# 查询员工是否存在
db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset')
cursor = db.cursor()
check_sql = "SELECT COUNT(*) FROM `employee_table` WHERE user_id = {}".format(id)
cursor.execute(check_sql)
result = cursor.fetchone()
if result[0] == 0:
QMessageBox.information(self, "提示", "该员工ID不存在")
db.close()
return
# 查询数据
db_sql = """
SELECT
a.user_id,
b.`name`,
b.phone,
get_time,
get_kg,
recycle_time,
recycle_kg,
CONCAT(ROUND(ROUND(yield_rate, 4) * 100, 2), '%') yield_rate
FROM
em_salary a
LEFT JOIN (SELECT user_id, `name`, phone FROM `employee_table`) b ON a.user_id = b.user_id
WHERE
a.user_id = {}
ORDER BY
get_time DESC
""".format(id)
cursor.execute(db_sql)
result = cursor.fetchall()
db.close()
if len(result) == 0:
QMessageBox.information(self, "提示", "该员工ID今日无工资记录")
return
self.query6_window.table_widget.setRowCount(0)
self.query6_window.table_widget.setColumnCount(len(result[0]))
self.query6_window.table_widget.setHorizontalHeaderLabels(
['员工编号', '姓名', '电话', '领取时间', '重量', '交鸡皮时间', '鸡皮重量', '出成率']
)
for row_num, row_data in enumerate(result):
self.query6_window.table_widget.insertRow(row_num)
for col_num, col_data in enumerate(row_data):
self.query6_window.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data)))
self.query6_window.show()
```
阅读全文