Table of Contents
Motivation
When dealing with databases you will need the ability to store certain dates and/or timestamps in your tables.
Let’s find out how you can do that in an SQLite database.
SQL table creation
SQLite has the data type TIMESTAMP for storing date-times
CREATE TABLE social_media (
social_media_id INTEGER,
insertion_date TIMESTAMP,
yt_subs INTEGER
fb_pg INTEGER
insta_pg INTEGER,
twitter_pg INTEGER)
Python code
In our Python code we will have to add this parameter to the connection
detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES
db_connection = sqlite3.connect('bmv.db', detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
cursor = db_connection.cursor()
cursor.execute('''SELECT * FROM social_media ORDER BY insertion_date DESC''')
rows = cursor.fetchall()
Now we will get datetime objects from our query.
Further Reading
This article is part of our SQL tutorial.