未定义“错误代码”,而请求看起来很好

时间:2019-05-02 11:58:52

标签: python mysql sql python-3.x mysql-python

在对话的某个时刻,用this chatbot,我想在a file actions.py中对MySQL数据库的流行表进行SQL查询,但是它返回终端,提示我错误在我的SQL查询上执行cursor.execute时的SQL语法:

        print("before execute")
        cursor = cnx.cursor()

        # add new booking
        add_booking = ("INSERT INTO reservations "
                      "(name_room, hour_start, hour_end) "
                      "VALUES (%s, %s, %s)")
        cursor.execute(add_booking)
        print("after execute")

我使用值blue 5-1-2019 2pm 30 minutes,但我不明白为什么会有语法错误。实际上,目标是使用以下结构为数据库输入名称(蓝色),输入日期和退出日期:

mysql> DESCRIBE reservations;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| name_room  | varchar(100) | NO   |     | NULL    |       |
| hour_start | varchar(100) | NO   |     | NULL    |       |
| hour_end   | varchar(100) | NO   |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+
3 rows in set (0.06 sec)

它始终有效,因为您可以从这里的旧数据中看到它:

mysql> SELECT * FROM reservations;
+-----------+---------------------------+---------------------------+
| name_room | hour_start                | hour_end                  |
+-----------+---------------------------+---------------------------+
| blue      | 2019-04-18T14:00:00+00:00 | 2019-04-18T14:30:00+00:00 |
| blue      | 2019-04-20T09:00:00+00:00 | 2019-04-20T09:30:00+00:00 |
| blue      | 2019-04-30T13:00:00+00:00 | 2019-04-30T13:30:00+00:00 |
| blue      | 2019-05-01T14:00:00+00:00 | 2019-05-01T14:30:00+00:00 |
+-----------+---------------------------+---------------------------+
4 rows in set (0.00 sec)

这是终端返回的内容

(staenv) C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack>python -m rasa_core_sdk.endpoint --actions actions
2019-05-01 17:46:10 INFO     __main__  - Starting action endpoint server...
2019-05-01 17:46:20 INFO     rasa_core_sdk.executor  - Registered function for 'action_joke'.
2019-05-01 17:46:20 INFO     rasa_core_sdk.executor  - Registered function for 'action_room'.
2019-05-01 17:46:20 INFO     __main__  - Action endpoint is up and running. on ('0.0.0.0', 5055)
inside run
before booking_answer
blue 5-1-2019 2pm 30 minutes
query_select_all:
SELECT * FROM reservations
date: 2019-05-01T14:00:00+00:00
date: 2019-05-01T14:30:00+00:00
cur_select_all:
CMySQLCursorBuffered: SELECT * FROM reservations
start_time:  2019-04-18T14:00:00+00:00
end_time:  2019-04-18T14:30:00+00:00
date: 2019-04-18T14:00:00+00:00
date: 2019-04-18T14:30:00+00:00
start_time:  2019-04-20T09:00:00+00:00
end_time:  2019-04-20T09:30:00+00:00
date: 2019-04-20T09:00:00+00:00
date: 2019-04-20T09:30:00+00:00
start_time:  2019-04-30T13:00:00+00:00
end_time:  2019-04-30T13:30:00+00:00
date: 2019-04-30T13:00:00+00:00
date: 2019-04-30T13:30:00+00:00
Hey, I just checked and the room is available :-)
booking_answer : True
before connexion
before execute
before execute
[2019-05-01 18:10:07,717] ERROR in app: Exception on /webhook [POST]
Traceback (most recent call last):
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\mysql\connector\connection_cext.py", line 395, in cmd_query
    raw_as_string=raw_as_string)
_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 '%s, %s, %s)' at line 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\actions.py", line 72, in run
    cursor.execute(add_booking)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\mysql\connector\cursor_cext.py", line 266, in execute
    raw_as_string=self._raw_as_string)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\mysql\connector\connection_cext.py", line 398, in cmd_query
    sqlstate=exc.sqlstate)
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 '%s, %s, %s)' at line 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask_cors\decorator.py", line 128, in wrapped_function
    resp = make_response(f(*args, **kwargs))
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\rasa_core_sdk\endpoint.py", line 86, in webhook
    response = executor.run(action_call)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\rasa_core_sdk\executor.py", line 177, in run
    events = action(dispatcher, tracker, domain)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\actions.py", line 75, in run
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
NameError: name 'errorcode' is not defined
2019-05-01 18:10:07 ERROR    flask.app  - Exception on /webhook [POST]
Traceback (most recent call last):
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\mysql\connector\connection_cext.py", line 395, in cmd_query
    raw_as_string=raw_as_string)
_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 '%s, %s, %s)' at line 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\actions.py", line 72, in run
    cursor.execute(add_booking)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\mysql\connector\cursor_cext.py", line 266, in execute
    raw_as_string=self._raw_as_string)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\mysql\connector\connection_cext.py", line 398, in cmd_query
    sqlstate=exc.sqlstate)
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 '%s, %s, %s)' at line 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\flask_cors\decorator.py", line 128, in wrapped_function
    resp = make_response(f(*args, **kwargs))
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\rasa_core_sdk\endpoint.py", line 86, in webhook
    response = executor.run(action_call)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\rasa_core_sdk\executor.py", line 177, in run
    events = action(dispatcher, tracker, domain)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\actions.py", line 75, in run
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
NameError: name 'errorcode' is not defined

1 个答案:

答案 0 :(得分:0)

尽管我们无法完全验证您的代码以检查所使用的查询是否正确,但错误似乎在以下行中:

File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\mysql\connector\connection_cext.py", line 395, in cmd_query raw_as_string=raw_as_string)_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 '%s, %s, %s)' at line 1

在您的堆栈跟踪中看到该错误之后,我检查了您的查询。我会写不同的话:

sql = "INSERT INTO reservations (name_room, hour_start, hour_end) VALUES (%s, %s, %s)"
val = ("blue", "5-1-2019", "2pm 30 minutes")
mycursor.execute(sql, val)

此外,出于良好编码习惯的考虑,请不要对应该具有日期时间值的属性使用varchar。这就是存在不同数据类型的原因。