I'm trying to create an in-memory database using sqlite3
in Python.
I created a function to create a db database file and store information in to it and that is working 100%.
But trying to connect with :memory:
I've faced some problems.
What I'm doing is:
import sqlite3
def execute_db(*args):
db = sqlite3.connect(":memory:")
cur = db.cursor()
data = True
try:
args = list(args)
args[0] = args[0].replace("%s", "?").replace(" update "," `update` ")
args = tuple(args)
cur.execute(*args)
arg = args[0].split()[0].lower()
if arg in ["update", "insert", "delete", "create"]: db.commit()
except Exception as why:
print why
data = False
db.rollback()
db.commit()
db.close()
return data
create name table
execute_db("create table name(name text)")
which returned
True
insert some information to this table
execute_db("insert into name values('Hello')")
which returned
no such table: name False
Why doesn't this work? It works when I use a file:
db = sqlite3.connect("sqlite3.db")