0

i use the python shell and execute an 'insert' sql, it appears "SyntaxError: invalid syntax", anyone could tell me what's the error in my MySQL statement? thanks

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

and desc the table 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)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Yx.Ac
  • 111
  • 2
  • 5

1 Answers1

0

SyntaxError is a Python error. The syntax error is in the datetime object creation:

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

tzinfo=<UTC> is a Python syntax error:

$ 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 how to provide a valid tzinfo argument.

Community
  • 1
  • 1
vz0
  • 32,345
  • 7
  • 44
  • 77