Python SQLite – Working with Date and DateTime
SQLite does not support built-in DateTime storage a class, but SQLite allows us to work with timestamp types. We can store and retrieve Python date and datetime information stored in the SQLite database tables by converting them to Python date and datetime types and vice-versa.
While inserting the datetime value, the python sqlite3 module converts the datetime into the string format. And when retrieving datetime values from SQLite tables, the sqlite3 module converts them into a string object. But we don’t want string type. We want the datetime to get stored in DateTime type. For that, we need to use detect_types as it takes PARSE_DECLTYPES and PARSE_COLNAMES as arguments in the connect method of the sqlite3 module.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Syntax: connect(‘DATABASE NAME’,detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
sqlite3.PARSE_DECLTYPES: The sqlite3 module parses the declared type for each column it returns then uses the type converters dictionary to execute the converter function registered for that type there.
sqlite3.PARSE_COLNAMES: The SQLite interface parses the column name for each column it returns. It will use the converters dictionary and then use the converter function found there to return the value.
Inserting Date and DateTime data
First, we need to import the datetime module and to get the current time and date information now() function can be used. Then they store the datetime information in a variable, so it can be used to insert datetime in the SQLite table. To store the datetime information in the table, we need to use the column datatype as ‘TIMESTAMP’.
Below code creates a database ‘StudentAssignment.db’ and a table ‘ASSIGNMENT’. The code also inserts the data into the table with datetime information.
Data Inserted Successfully !
Retrieve Date and DateTime data
In order to retrieve the stored datetime information from database tables, we can simply use the select query and can access the individual rows data. Here for retrieval of data and to check the datatypes in which the data is stored, we need to use the detect_types as arguments in the connect method of the sqlite3 module.
The below code retrieves the stored record from the ‘ASSIGNMENT’ table. The record contains the ‘datetime’ type information.
As we can see in the output when we retrieve the submission data of students from ‘ASSIGNMENT’ table. We accessed each row of the table and printed them in a specific message. First-line contains the student name and the ID, on the second line we printed the datetime on which the student submitted the assignment, and on the third line we printed the type of the stored data, which is the type of datetime we stored in the table i.e., ‘datetime.datetime’.