I'm using Python MySQLDB, and I wish to place this into DATETIME area in Mysql . How do you do this with cursor.execute?

To transform from the UNIX timestamp to some Python datetime object, use datetime.fromtimestamp() (documentation).

>>> from datetime import datetime
>>> datetime.fromtimestamp(0)
datetime.datetime(1970, 1, 1, 1, 0)
>>> datetime.fromtimestamp(1268816500)
datetime.datetime(2010, 3, 17, 10, 1, 40)

From Python datetime to UNIX timestamp:

>>> import time
>>> time.mktime(datetime(2010, 3, 17, 10, 1, 40).timetuple())

You should use the FROM_UNIXTIME MySQL function:

#import MySQLdb as mysql
import mysql.connector as mysql

if __name__ == '__main__':
    cnx = mysql.connect(user='root')
    cur = cnx.cursor()

    cur.execute("SELECT FROM_UNIXTIME(%s)", (1268811665,))
    print cur.fetchall()

The output (should you save the above mentioned to epoch.py):

$ python epoch.py 
[(datetime.datetime(2010, 3, 17, 8, 41, 5),)]

You should use the FROM_UNIXTIME inside your Place or any other DML SQL claims.


I simply did this:

datetime.datetime.now() ...place that in to the column.