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?

2
  • What do you mean by browse? Do you mean running queries on it? Commented May 15, 2015 at 6:38
  • yeah, but on the fly, i.e. in debugging. Commented May 15, 2015 at 6:44

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.