MySQL语句中的错误是什么?

时间:2014-05-19 09:45:37

标签: python mysql

我使用python shell并执行' insert' sql,它出现"语法错误:语法无效",任何人都可以告诉我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

和desc表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)

1 个答案:

答案 0 :(得分:0)

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

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

tzinfo=<UTC>是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参数。