I am aware of this similar question on SO which is basically asking about the same thing. However, I seems to be getting an error message. Let me explain.
In order to test the idea of Storing and Retrieving Python Date object with Sqlite3 database, I have created this small test script:
import sqlite3
import datetime
conn = sqlite3.connect("test.db")
conn.execute('''CREATE TABLE TEST
(
ID TEXT PRIMARY KEY NOT NULL,
DATE TIMESTAMP
);''')
conn.commit()
mydate = datetime.date(2014,4,28)
myid = 'test'
conn.execute("INSERT INTO TEST (ID,DATE)\
VALUES(?,?)",[myid,mydate])
conn.commit()
conn.close()
conn = sqlite3.connect("test.db")
cursor = conn.execute("SELECT ID,DATE from TEST")
for row in cursor:
retrievedDate = row[1]
print retrievedDate,type(retrievedDate)
conn.close()
The code works but the retrieved date is in unicode
.
I found out from the above link that without the detect_types=sqlite3.PARSE_DECLTYPES
parameters sqlite will return unicode. So, I have changed the code like this:
...
conn = sqlite3.connect("test.db",detect_types=sqlite3.PARSE_DECLTYPES)
cursor = conn.execute("SELECT ID,DATE from TEST")
for row in cursor:
retrievedDate = row[1]
print retrievedDate,type(retrievedDate)
...
But now it's giving me this error message:
File "C:\Python27\lib\sqlite3\dbapi2.py", line 66, in convert_timestamp
datepart, timepart = val.split(" ")
ValueError: need more than 1 value to unpack
What is causing the problem?