mardi 26 janvier 2016

reopen a database in sqllite with python report error

I'm trying to learn about operating sqlite in python. I encountered a question that : I open a database named "test.db" in current work dir,then closed it, then reopen it immediately without close the program, but it fails with error message sqlite3.OperationalError: no such table: stocks. So I try to create a database in memory with name :memory:,and the error message is the same.

Then I googled this ,found that if the database's name is a absolute path the error disapeared. But I donot know why? And how do with :memory:?

python code is that :

import sqlite3

dbName ="test.db
conn = sqlite3.connect(dbName)

c = conn.cursor()
c.execute('CREATE TABLE stocks (date text, trans text, symbol text, qty real, p$
c.execute("INSERT INTO stocks VALUES ('2016-01-25','BUY','RHAT',100,35.14)")
conn.commit()
conn.close()

conn = sqlite3.connect(dbName)
c = conn.cursor()
t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
print(c.fetchone())
conn.close()

Aucun commentaire:

Enregistrer un commentaire