A simple example:
import sqlite3, datetime, csv
import pandas.io.sql as sql
from dateutil.parser import parse
my_db = 'test_db.db'
connection=sqlite3.connect(my_db,detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
cursor = connection.cursor()
cursor.execute('''CREATE TABLE test_table (Id INTEGER PRIMARY KEY, Date DATE);''')
date_str = '1/1/2011'
date_parsed = parse(date_str)
cursor.execute('INSERT into test_table (Id, Date) values(?,?)',(1,date_parsed))
cursor.execute('SELECT * FROM test_table')
yields:
ValueError: invalid literal for int() with base 10: '01 00:00:00'
I’m simply trying to have the SQL db return my dates in datetime format so I can then perform filtered queries on them.
I’ve already read this related post for reference:
Inside your
CREATE TABLE, you’re usingDate DATEThe problem with this is that that tries to map to
datetime.datewhich your format isn’t, if you change your format to use aTIMESTAMP, then it works correctly and tries to make it adatetime.datetime…