Trying to store datetimes in my database project using python and sqlite3. Here's what I have so far...
import sqlite3
def connect():
conn = sqlite3.connect("road_works.db")
cur = conn.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS road_works(id INTEGER PRIMARY
KEY, location TEXT, client TEXT, start_date TEXT, end_date TEXT)""")
conn.commit()
conn.close()
def insert(location, client, start_date, end_date):
conn = sqlite3.connect("road_works.db")
cur = conn.cursor()
cur.execute("INSERT INTO road_works VALUES (NULL, ?, ?, ?, ?)",
(location, client, start_date, end_date))
conn.commit()
conn.close()
insert("M25", "Lindor", "2019-03-16 20:00", "2019-07-16 06:00")
At the minute the database takes start_date and end_date as text. I want to change that to dates that I can then start doing calculations with like counting down the days/nights until the job is finished etc. The user will also be inputing the dates and times so I cant use datetime.datetime.now()/today() I have been reading into datetime but its more advanced than I'm used to if anyone could explain it better I would appreciate it.
I have been searching for the solution for a few days this is my first project it may be a little out of my league but I appreciate your attention.
Thanks :)
fixed! Thanks for the help!
def insert(location, client, start_date, end_date):
conn = sqlite3.connect("road_works.db")
cur = conn.cursor()
cur.execute("INSERT INTO road_works VALUES (NULL, ?, ?, ?, ?)",
(location, client, start_date, end_date))
start_date = datetime.strptime(start_date, "%Y-%m-%d %H:%M")
end_date = datetime.strptime(end_date, "%Y-%m-%d %H:%M")
conn.commit()
conn.close()