0

I'm trying to insert my datettime object into MySQL

>>> t
datetime.datetime(2013, 5, 21, 19, 33, 36, tzinfo=tzutc())
>>> cursor.execute('INSERT INTO tweets(created_at) VALUES ({created_at})'.format(created_at=t))

Error I'm getting:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.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 '19:33:36+00:00)' at line 1")
>>> 

MySQL:

mysql> DESCRIBE mytable;
+-------------+---------------------+------+-----+---------+-------+
| Field       | Type                | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+-------+

| created_at  | datetime            | YES  |     | NULL    |       |

+-------------+---------------------+------+-----+---------+-------+
3
  • 1
    Try:>>> cursor.execute('INSERT INTO tweets (created_at) VALUES ("{created_at}")'.format(created_at=t)) Note the space and the " Commented May 21, 2013 at 20:23
  • I don't know anything about databases (yet), but the string formatting you're using will implicitly call str on your datetime object and use that string to substitute for {created_at}. From the error, it looks like that string is what is causing mysql to choke. Commented May 21, 2013 at 20:23
  • Nice! Well I put it as an answer, I'll be glad if your aprove it! Commented May 21, 2013 at 20:31

2 Answers 2

4

Use parametrized sql instead of string formatting and manual quoting:

cursor.execute('INSERT INTO tweets(created_at) VALUES (%s)', [t])

It's easier, and helps prevent sql injection.

Sign up to request clarification or add additional context in comments.

Comments

-1

Try to insert a space after your table name and to put your text inside "", like this:

cursor.execute('INSERT INTO tweets (created_at) VALUES ("{created_at}")'.format(created_at=t)) 

3 Comments

Using the format string method should be avoided. Variables don't get escaped and you'll have SQL injection issues. See Django's docs on the issue.
I do agree with you, but in his case this variable comes from internal code only. Is this subject to injection too? I'm not being ironic, I'm realy asking you, I'm not a literate in sanitized coding.
It's best to always keep things standardised and to do things the correct way every time. It will avoid confusion, and the possibility that the code may be used or copied without thinking into something that might introduce issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.