I’ve been following the Python examples @ http://docs.python.org/2/library/sqlite3.html#sqlite3
import sqlite3
conn = sqlite3.connect("tweet.db")
c = conn.cursor()
for row in c.execute('SELECT * FROM stocks ORDER BY price'):
print row
conn.close()
But the rows aren’t printing unless I have:
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
I’m using the same database and I’ve inserted data before trying to select it.
What am I doing wrong?
You need to commit your inserts:
Only when you commit your transactions does sqlite write the rows to the database permanently. Quoting from the documentation:
Alternatively, you could set the connection isolation level to
Noneto auto-commit changes.