我正嘗試在Python中使用sqlite3
創建內存數據庫。如何使用sqlite創建內存數據庫?
我創建了一個函數來創建一個數據庫數據庫文件並存儲信息到它並且工作100%。
但試圖連接:memory:
我遇到了一些問題。
我在做什麼是:
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
創建名稱表
execute_db("create table name(name text)")
其返回
True
插入一些信息,該表
這回no such table: name False
execute_db("insert into name values('Hello')")
爲什麼不工作的呢?它的工作原理,當我使用一個文件:
db = sqlite3.connect("sqlite3.db")
爲什麼在更改數據時雙'db.commit()'? –
@MartijnPieters哦,我的謝謝你。 – Deounix