3

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?

atbug
  • 818
  • 6
  • 26

1 Answers1

3

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"

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)
bprasanna
  • 2,423
  • 3
  • 27
  • 39