I am trying to import a list of thousands of objects from a CSV. When I import, everything stores correctly, except date and datetime objects. These objects simply become null in my database.
One example of a datetime object is the nrel_date_updated key. Interestingly, if I put a print statement in my import script to print nrel_date_updated, it correctly prints the unicode of the date. Nonetheless, the date becomes null in the database.
I have tried adding nrel_date_updated outside of the keyString and valString loop, so that it is not automatically concatenated as a string, but I got the same result- null in the database.
Any ideas what my problem is?
con = sqlite3.connect("../CCdb.sqlite3")
cur = con.cursor()
f = open('alt_fuel_stations.csv', 'rt')
try:
reader = csv.DictReader(f)
for row in reader:
keyString = ""
valString = ""
for key,value in row.iteritems():
keyString += key + ","
if (key != 'latitude' and key != 'longitude' and key != 'federal_agency_id'
and key != 'ev_dc_fast_num' and key != 'ev_level1_evse_num' and key != 'ev_level2_evse_num'):
valString += "'" + value + "',"
else:
if value=="":
valString += "null,";
else:
valString += value + ",";
print valString;
valString = valString[:-1];
keyString = keyString[:-1];
cur.execute("INSERT INTO chargecamp_station" +
"(" + keyString + ")" + "VALUES " +
"(" + valString + ");")
con.commit()
finally:
f.close()