Date and time types in Python

The date and time interfaces to MySQL for Python are very robust. As a consequence, as long as the values you pass make sense within the above parameters for formatting, neither MySQL nor Python will throw an error about aspects such as the Python data type. For example, if we go back to the yeareg table, we can pass integers and strings to it through Python without issue:

import MySQLdb
mydb = MySQLdb.connect('localhost', 'skipper', 'secret', 'datetime')
cursor = mydb.cursor()
x = 2012
statement = "INSERT INTO yeareg(year) VALUES('%s')" %(x)
results = cursor.execute(statement)
x = str(x)
statement = "INSERT INTO yeareg(year) VALUES('%s')" %(x)
results = cursor.execute(statement)

In the first statement, we pass a string. ...

Get MySQL for Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.