mysql - 如何在 MySQL 查询和 Python 中使用 WHERE & AND
问题描述
早上好,我的 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 运行相同的查询,用户必须输入日期和限制,这是查询提出的:
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
有什么不对劲吗?
解决方案
似乎您错过了日期值周围的引号,但无论如何您应该避免在查询代码中连接字符串 - 改用参数(并使用 var 仅用于限制或完整的参数查询使用过程)
cursor.execute("SELECT voltage, time FROM patient_results_tbl WHERE date = %s AND patient_id = %s LIMIT " + lim , (start,pat_id))
推荐阅读
- java - WEKA 交叉验证线性回归 - 我可以获得 RMSPE 吗?
- animation - 如何在骨架中找到根节点的全局位置?
- user-agent - 如何使用导航器检测勇敢
- python - 断言失败 TensorFlow
- pepper - 自从 Softbank Robotics Store 关闭后,我如何向 Pepper 添加其他语言?
- redis - 如何在 Redis 中缓存整个 next.js 的 HTML 页面?
- c# - 如何基于另一个 DropDownList 在 DropDownList 中添加值
- c++ - 在 C++ 中输出 Unicode 棋子
- php - PHP:当一个或多个表单字段为空(多个字段)时,如何正确显示错误消息?
- javascript - 如果未满足最小和最大条件,则阻止表单提交