0

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()
sdotslezek
  • 483
  • 1
  • 6
  • 9
  • what is the output if you print those values to the console `heyString, valString` ? – PepperoniPizza Jun 12 '14 at 03:40
  • check the answer to this http://stackoverflow.com/questions/23210847/mysql-returned-datatype-and-python-outputs-different-type-for-different-function – user-2147482637 Jun 12 '14 at 03:42
  • Here is a keyString print: nrel_date_last_confirmed,nrel_id,city,geocode_status,nrel_status_code,nrel_date_opened,state,ev_network_web,cards_accepted,ev_dc_fast_num,latitude,ev_other_evse,access_time,zip_code_plusfour,ev_level2_evse_num,phone,access_group,intersection_directions,federal_agency_id,name,federal_agency_name,country,owner_type_code,ev_network,longitude,ev_level1_evse_num,nrel_date_updated,ev_connector_types,zip_code,street_address, – sdotslezek Jun 12 '14 at 05:09
  • and a valString print: '6/3/2014','60337','Oakland','GPS','E','1/31/2012','CA','','',1,37.820419,'','','0',1,'510-588-2200','Private','',null,'Nissan of Oakland','','USA','P','',-122.261344,null,'2014-06-03 03:45:01 UTC','J1772 CHADEMO','94611','2735 Broadway', – sdotslezek Jun 12 '14 at 05:10
  • (values are pulled from a gov database and format is not changed) – sdotslezek Jun 12 '14 at 05:10

0 Answers0