Python操作mysql数据库出现pymysql.err.ProgrammingError1064错误

操作:

def get_c1_data():
    sql = "select sum(confirm),"\
          "(select suspect from history order by ds desc limit 1),"\
          "sum(heal),"\
          "sum(dead)"\
          "from details"\
          "where update_time=(select update_time from details order by update_time desc limit 1)"
    res = query(sql)
    return res[0]

if __name__ == "__main__":
    print(get_c1_data())

错误如下:

C:\Users\bellis\AppData\Local\Programs\Python\Python37\python.exe E:/pycharm-workspace/Cov/utils.py
Traceback (most recent call last):
  File "E:/pycharm-workspace/Cov/utils.py", line 40, in <module>
    print(get_c1_data())
  File "E:/pycharm-workspace/Cov/utils.py", line 35, in get_c1_data
    res = query(sql)
  File "E:/pycharm-workspace/Cov/utils.py", line 23, in query
    cursor.execute(sql, args)
  File "C:\Users\bellis\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\cursors.py", line 163, in execute
    result = self._query(query)
  File "C:\Users\bellis\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\cursors.py", line 321, in _query
    conn.query(q)
  File "C:\Users\bellis\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\connections.py", line 505, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "C:\Users\bellis\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\connections.py", line 724, in _read_query_result
    result.read()
  File "C:\Users\bellis\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\connections.py", line 1069, in read
    first_packet = self.connection._read_packet()
  File "C:\Users\bellis\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\connections.py", line 676, in _read_packet
    packet.raise_for_error()
  File "C:\Users\bellis\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\protocol.py", line 223, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "C:\Users\bellis\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\err.py", line 107, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "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 '=(select update_time from details order by update_time desc limit 1)' at line 1")

看错误以为是sql语句写错了,可是找了半天也没发现,在网上看了好多解决方法也都不行,后来有人说是sql语句中的换行符有问题,试着换了一下还真就可以了,建议换行符最好用三个引号来,如下:

sql = """
	sql语句
"""

你可能感兴趣的:(Python)