David White
David White

Reputation: 113

No error, but no data written to SQLite DB

I have a python list called result that contains 7 items...

print result

returns

[u'2013:11:29', u'17:01:11', u'Apple', u'iPhone 5', -36.57033055555556, 174.68374722222222, '5095c554fef7d990a2c57e0e12b18854']

I have this code setting up my DB, and writting the result into the DB

conn = sqlite3.connect('geopic.sqlite')
cur = conn.cursor()
cur.execute('''DROP TABLE IF EXISTS Results''')
cur.execute('''CREATE TABLE Results (Date DATE, Time TIME, Make TEXT, Model TEXT, Latitude FLOAT, Longitude FLOAT, Hash VARCHAR(128))''')
for result in results:
    if result == None:
        continue
    else:
        print result  #this prints as expected
        cur.execute('''INSERT INTO Results (Date, Time, Make, Model, Latitude, Longitude, Hash) VALUES (?,?,?,?,?,?,?)''', result)

This runs with no errors, but nothing gets written into the database. I'm stumped as to why!

Upvotes: 1

Views: 281

Answers (2)

David White
David White

Reputation: 113

OK - Simple Answer - I forgot to use

conn.commit()

To write the changes to the DB.

DOH!

Upvotes: 0

Saleem
Saleem

Reputation: 8978

Append conn.commit() to end of your code.

Example:

for result in results:
    if result == None:
        continue
    else:
        print result  #this prints as expected
        cur.execute('''INSERT INTO Results (Date, Time, Make, Model, Latitude, Longitude, Hash) VALUES (?,?,?,?,?,?,?)''', result)
conn.commit()

Upvotes: 1

Related Questions