如何在python中浏览内存中的sqlite数据库 [英] How to browse an in memory sqlite database in python

查看:33
本文介绍了如何在python中浏览内存中的sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用具有以下构造函数的内存 sqlite 数据库:

I need to use an in memory sqlite database with the following constructor:

db = sqlite3.connect(':memory:')

但是在调试的时候,我觉得很不方便,因为不像基于文件的数据库,我无法在调试器中浏览数据库.

But in debugging, I find it very inconvenient because unlike file-based database, I cannot browse the database in the debugger.

有没有办法即时浏览这个数据库?

Is there a way to browse this database on the fly?

推荐答案

您可以在调试器中编写 Python 脚本来执行任何查询.例如,考虑以下程序:

You can write python scripts in debugger to perform any queries. For example, consider the following program:

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)后,我们可以编写如下查询:

Once we enter debugging (pdb), we can write queries like the following:

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)

这篇关于如何在python中浏览内存中的sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆