如何在 python 中浏览内存中的 sqlite 数据库
How to browse an in memory sqlite database in python
我需要使用具有以下构造函数的内存中 sqlite 数据库:
db = sqlite3.connect(':memory:')
但是在调试中,我觉得很不方便,因为不像基于文件的数据库,我不能在调试器中浏览数据库。
有没有办法即时浏览这个数据库?
您可以在调试器中编写 python 脚本来执行任何查询。
例如,考虑以下程序:
import pdb
import sqlite3
con = sqlite3.connect(':memory:')
cur = con.cursor()
cur.execute('create table abc (id int, sal int)')
cur.execute('insert into abc values(1,1)')
cur.execute('select * from abc')
data = cur.fetchone()
print (data)
pdb.set_trace()
x = "y"
进入调试(pdb)后,我们可以编写如下查询:
D:\Sandbox\misc>python pyd.py
(1, 1)
> d:\sandbox\misc\pyd.py(11)<module>()
-> x = "y"
(Pdb) cur.execute('insert into abc values(2,2)')
<sqlite3.Cursor object at 0x02BB04E0>
(Pdb) cur.execute('insert into abc values(3,3)')
<sqlite3.Cursor object at 0x02BB04E0>
(Pdb) cur.execute('select * from abc')
<sqlite3.Cursor object at 0x02BB04E0>
(Pdb) rows = cur.fetchall()
(Pdb) for row in rows: print (row)
(1, 1)
(2, 2)
(3, 3)
(Pdb)
我需要使用具有以下构造函数的内存中 sqlite 数据库:
db = sqlite3.connect(':memory:')
但是在调试中,我觉得很不方便,因为不像基于文件的数据库,我不能在调试器中浏览数据库。
有没有办法即时浏览这个数据库?
您可以在调试器中编写 python 脚本来执行任何查询。 例如,考虑以下程序:
import pdb
import sqlite3
con = sqlite3.connect(':memory:')
cur = con.cursor()
cur.execute('create table abc (id int, sal int)')
cur.execute('insert into abc values(1,1)')
cur.execute('select * from abc')
data = cur.fetchone()
print (data)
pdb.set_trace()
x = "y"
进入调试(pdb)后,我们可以编写如下查询:
D:\Sandbox\misc>python pyd.py
(1, 1)
> d:\sandbox\misc\pyd.py(11)<module>()
-> x = "y"
(Pdb) cur.execute('insert into abc values(2,2)')
<sqlite3.Cursor object at 0x02BB04E0>
(Pdb) cur.execute('insert into abc values(3,3)')
<sqlite3.Cursor object at 0x02BB04E0>
(Pdb) cur.execute('select * from abc')
<sqlite3.Cursor object at 0x02BB04E0>
(Pdb) rows = cur.fetchall()
(Pdb) for row in rows: print (row)
(1, 1)
(2, 2)
(3, 3)
(Pdb)