This lesson demonstrates how to work with SQLite date and timestamp types in Python and vice-versa. Most of the time, we need to insert Python date or DateTime value into an SQLite table. Also, we need to read the SQLite date and timestamp values stored in the SQLite3 database and convert them into Python date and DateTime types.
Also Read:
Table of contents
Prerequisites
Before executing the following program, please make sure you have an SQLite table with a timestamp as a column from which you want to retrieve/insert data.
For this lesson, I am using the ‘new_developers’ table present in my SQLite database.
If a table is not present in your SQLite database, then please refer to the following articles: –
Python example to insert/Retrieve DateTime from SQLite table
In a usual scenario, when you execute the insert query with the DateTime object, the Python sqlite3 module converts it into a string format instead of an actual DateTime. And when you run a SELECT query from Python to read DateTime values from SQLite table, the sqlite3 module will convert it into a string object. Let’s understand this scenario with a simple example.
Output:
Connected to SQLite Developer added successfully Mark joined on 2019-06-28 21:12:35.799206 joining date type is <class 'str'> sqlite connection is closed
As we can see, we inserted a date object, but when we retrieved the details from a table, we got a string type. But we don’t want string type. We want the DateTime type so we can directly use it.
To solve this problem, use detect_types as PARSE_DECLTYPES and PARSE_COLNAMES as arguments in the connect method of the sqlite3 module.
sqlite3.PARSE_DECLTYPES
If you use this parameter in the connect method, then the sqlite3 module parses the declared type for each column it returns.
It will parse the declared type then use the type converters dictionary to execute the converter function registered for that type there.
sqlite3.PARSE_COLNAMES
If you use this parameter in the connect method, then 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.
Let see the example now. In this example, when we read DateTime from the SQLite table we must get the joining date type as a datetime.
Output:
Connected to SQLite Developer added successfully Mark joined on 2019-06-28 20:57:32.352790 joining date type is <class 'datetime.datetime'> sqlite connection is closed
As you can see when we retrieved the joining date from SQLite table and we got the result in datetime.datetime type.
Next Steps:
To practice what you learned in this article, Solve a Python SQLite Exercise project to practice database operations.

Why we creating table every time when calling this function?
Is there a way to extend the PARSE_COLNAMES’ dictionary to recognize other conventions?
Can you add examples of query too? For eg select records from table where timestamp < xxxx.
Hi!
Very useful article! I suppose it will be great to append an example of comparing datetime fields like in “select-where” instruction. I.e. select all joined before the estimated date. It should be helpful for me.
Hey Vasil Kolomiets, Thank you for letting me know, I will add it.
Hello Vishal, you declared a column as timestamp but I can’t find any documentation in SQLite or anywhere that talks about that type, or a list of types like that. Can you provide any guidance on it?
Thanks!
Awesome web site.. I want to search an sqlite3 database with datetime criteria; Like search between datetime A and datetime B. (the datetime is already in the first column of the database.) The string retrieved from the database does not respond to ” obviously. Could you give me some direction?
Hey Jhon, Thank you. Please refer to this stackoveflow question SQLite Select between dates
Thank you so much Vishal for this article, it is very enlightening.
One quick question: if I don’t want to use the method datetime.datetime.now() as input for the new developer’s data, but would like to use an actual date like 10-17-2019, how could I do this?
You need to use a single quotation ‘ contain your date string.
for example in our demo you can pass like this.
this will translate into:
Let me know your results