首页 > 解决方案 > 如何在 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

有什么不对劲吗?

标签: mysqlpython-3.xuser-interfacetkinter

解决方案


似乎您错过了日期值周围的引号,但无论如何您应该避免在查询代码中连接字符串 - 改用参数(并使用 var 仅用于限制或完整的参数查询使用过程)

cursor.execute("SELECT voltage, time FROM patient_results_tbl WHERE date = %s AND patient_id = %s LIMIT " + lim , (start,pat_id))

推荐阅读