【问题标题】:What's the error in the MySQL statement?MySQL语句中的错误是什么?
【发布时间】:2014-07-07 05:23:28
【问题描述】:

我使用 python shell 并执行一个'insert' sql,它出现“SyntaxError: invalid syntax”,任何人都可以告诉我我的 MySQL 语句中的错误是什么?谢谢

cursor.execute("insert into monitor_task (job_id, task_id, host, port, active, last_attempt_time, last_status last_message, last_success_time, last_metrics, last_metrics_raw) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", [1L, 0, 'hh-hadoop-srv-ct01.bj', 11101, True, datetime.datetime(1970, 1, 1, 0, 0, tzinfo=<UTC>), 2, '', datetime.datetime(1970, 1, 1, 0, 0, tzinfo=<UTC>), '', ''])
  File "<console>", line 1
    cursor.execute("insert into monitor_task (job_id, task_id, host, port, active, last_attempt_time, last_status last_message, last_success_time, last_metrics, last_metrics_raw) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", [1L, 0, 'hh-hadoop-srv-ct01.bj', 11101, True, datetime.datetime(1970, 1, 1, 0, 0, tzinfo=<UTC>), 2, '', datetime.datetime(1970, 1, 1, 0, 0, tzinfo=<UTC>), '', ''])
                                                                                                                                                                                                                                                                                                                               ^
SyntaxError: invalid syntax

并描述表 monitor_task:

mysql> desc monitor_task;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| id                | int(11)      | NO   | PRI | NULL    | auto_increment |
| job_id            | int(11)      | NO   | MUL | NULL    |                |
| task_id           | int(11)      | NO   |     | NULL    |                |
| host              | varchar(128) | NO   | MUL | NULL    |                |
| port              | int(11)      | NO   |     | NULL    |                |
| active            | tinyint(1)   | NO   |     | NULL    |                |
| last_attempt_time | datetime     | NO   |     | NULL    |                |
| last_status       | int(11)      | NO   |     | NULL    |                |
| last_message      | varchar(128) | NO   |     | NULL    |                |
| last_success_time | datetime     | NO   |     | NULL    |                |
| last_metrics      | longtext     | NO   |     | NULL    |                |
| last_metrics_raw  | longtext     | NO   |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+
12 rows in set (0.00 sec)

【问题讨论】:

  • 箭头指向你出错的地方。你不能说tzinfo=&lt;UTC&gt;,你需要一个实际的 tzinfo 对象。
  • last_status last_message 语法错误。必须有一个逗号。即last_status,last_message
  • @suhail 是的,我已经修复了语法错误。非常感谢。

标签: python mysql


【解决方案1】:

SyntaxError 是 Python 错误。语法错误在datetime对象创建中:

datetime.datetime(1970, 1, 1, 0, 0, tzinfo=<UTC>)

tzinfo=&lt;UTC&gt; 是 Python 语法错误:

$ python
Python 2.7.3 (default, Mar 13 2014, 11:03:55) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime(1970, 1, 1, 0, 0, tzinfo=<UTC>)
  File "<stdin>", line 1
    datetime.datetime(1970, 1, 1, 0, 0, tzinfo=<UTC>)
                                               ^
SyntaxError: invalid syntax

You can find here 如何提供有效的tzinfo 参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2012-05-31
    • 2015-08-08
    相关资源
    最近更新 更多