我想插入我的datettime對象到MySQL插入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))
錯誤我得到datetime.datetime對象:
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 | |
+-------------+---------------------+------+-----+---------+-------+
嘗試:>>> cursor.execute('INSERT INTO tweets(created_at)VALUES(「{created_at}」)'.format(created_at = t))注意空間和「 –
我不知道任何關於數據庫(但),但你使用的字符串格式會隱式地在你的datetime對象上調用'str',並使用該字符串來替換'{created_at}'。從錯誤中看,這個字符串就是導致mysql 0123。 – mgilson
你是那麼對!@古斯塔沃巴爾加斯,謝謝你的作品 – Vor