【发布时间】:2021-12-25 20:35:59
【问题描述】:
早上好,我的 MySQL 工作台架构中有一个表,如下所示,我正在直接从工作台运行查询:
SELECT voltage, time FROM patient_results_tbl WHERE date ='2021-11-05' AND patient_id = 260939631 LIMIT 4;
# patient_id, voltage, time, date
'260939631', '98', '18:51:46', '2021-11-13'
'260939631', '98', '18:51:47', '2021-11-13'
'260939631', '111', '18:51:48', '2021-11-13'
'260939631', '106', '18:51:49', '2021-11-13'
'260939631', '115', '18:51:50', '2021-11-13'
'260939631', '114', '18:51:51', '2021-11-13'
'785393661', '5', '18:57:32', '2021-11-13'
'785393661', '317', '18:57:33', '2021-11-13'
'785393661', '321', '18:57:34', '2021-11-13'
'785393661', '325', '18:57:34', '2021-11-13'
'785393661', '328', '18:57:35', '2021-11-13'
'785393661', '337', '18:57:35', '2021-11-13'
输出正常
# patient_id, voltage, time, date
'785393661', '317', '18:57:33', '2021-11-13'
'785393661', '321', '18:57:34', '2021-11-13'
'785393661', '325', '18:57:34', '2021-11-13'
'785393661', '328', '18:57:35', '2021-11-13'
现在我想使用 Tkinter GUI 运行相同的查询,用户必须输入日期和 LIMIT,这是提出的查询:
def analyze_voltage_time():
lim = limit_var.get()
start = s_date_var.get()
end = e_date_var.get()
_id = id_selector.get()
pat_id = _id[:9]
query = "SELECT voltage, time FROM patient_results_tbl WHERE date = "+start +" AND patient_id ="+pat_id + " LIMIT "+lim
mycursor.execute(query)
result = mycursor .fetchall()
voltage, time = list(zip(*result))
for volts in voltage:
voltage_container.append(volts)
for tim in time:
time_container.append(str(tim))
my_ax1.plot(time_container, voltage_container)
my_canvas1.draw_idle()
analyse_btn['state'] = DISABLED
当我运行时出现错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Kennedy Mulenga\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection_cext.py", line 513, in cmd_query
self._cmysql.query(query,
_mysql_connector.MySQLInterfaceError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND patient_id =785393661 LIMIT' at line 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Kennedy Mulenga\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\Kennedy Mulenga\Desktop\Level 5\18136709_BIT_280_Arduino_ECG_Project\18136709_ECG_LArdmon_APP_Final.py", line 260, in analyze_voltage_time
mycursor.execute(query)
File "C:\Users\Kennedy Mulenga\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\cursor_cext.py", line 269, in execute
result = self._cnx.cmd_query(stmt, raw=self._raw,
File "C:\Users\Kennedy Mulenga\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection_cext.py", line 518, in cmd_query
raise errors.get_mysql_exception(exc.errno, msg=exc.msg,
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND patient_id =785393661 LIMIT' at line 1
有什么地方做得不对吗?
【问题讨论】:
-
按照教程更新代码以对所有数据位置使用参数——这将使代码更易读、更安全,并避免查询中的琐碎错误。错误很可能出现在生成的字符串中“AND”的left。例如,我怀疑字符串可能看起来像
.. WHERE date = AND participant_id =785..。 -
无论如何,查看发送到 MySQL 的 actual 字符串将清楚地显示查询错误的地方。 “调试细节”涉及识别(并显示)这个值。
标签: mysql python-3.x user-interface tkinter