【发布时间】:2021-11-15 17:21:05
【问题描述】:
import sqlite3
conn = sqlite3.connect(":memory:")
cur = conn.cursor()
cur.executescript("""
create table person(
firstname,
lastname,
age
);
create table book(
title,
author,
published
);
insert into book(title, author, published)
values (
'Dirk Gently''s Holistic Detective Agency',
'Douglas Adams',
1987
);
""")
cur.execute("""
SELECT * FROM book
""")
print(conn)
conn.commit()
x=cur.fetchall()
print(x)
我参考了这本书,它给出了这样的代码。在这里,您可以看到提到的数据库名称为“:memory:”,这是什么意思?我以为它用于在 RAM 中创建临时数据库,但是当我执行此代码时,它正在运行,但是当 我 fetchall() 并打印它时,它显示 empty list 。什么是临时数据库的使用。另外,我在这里没有使用正常的 execute 方法。我在这里使用 exeutesscript 方法。你能帮帮我吗?
【问题讨论】:
-
别的意思
-
这里我使用的是executionscript方法,而不是execute方法。
-
您在 Book 表中插入了一条记录,然后您从 Person 表中进行选择,该表确实是空的,因为您没有向其中插入任何记录。
-
是的,我更正了。但我想在这里澄清一下临时数据库。
标签: python python-3.x database sqlite