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. In the second, an integer. Yet both statements will be executed by MySQL without issue.
mysql> SELECT * FROM yeareg; +----+------+ | ID | year | +----+------+ | 1 | 1923 | | 2 | 2023 | | 3 | 2013 | | 3 | 2013 | +----+------+ 4 rows in set...