1

I have a function in python which connects to sqlite DB which has 20k rows and just executes a simple select query as below

def viewdata(mul):
    conn = sqlite3.connect("mynew.db")
    cursor = conn.cursor()
    cursor.execute(("SELECT ad,abd,acd,ard FROM allrds WHERE mul<=?ORDER BY mul DESC LIMIT 1"),(mul,))
    data = [i for i in cursor.fetchall()]
    conn.close()
    return data

its kind of slow, so i want to move this into in memory Database of SQLite, how can i copy this existing DB to in memory DB and make a connection and fetch the data and close it once the operations are over. Is there anything different i need to do when connecting to memory databases? are the select queries executed the same way like we do for on disk DB? Can someone please give me an example function

computernoob
  • 420
  • 4
  • 18
  • 1
    this may provide most of the needed information : https://stackoverflow.com/questions/3850022/how-to-load-existing-db-file-to-memory-in-python-sqlite3 – olly_uk Feb 15 '19 at 09:38
  • it does not have the information for executing our queries – computernoob Feb 15 '19 at 09:41
  • once the db is in memory you can query like any other sqlite db object – olly_uk Feb 15 '19 at 09:42
  • 1
    Does this mul column have an index? If not, creating one will be a big speedup for the query. Probably more so than trying to load it all into memory will give. – Shawn Feb 15 '19 at 10:08
  • Yes, that will work, but i cant see any doc or function which explains the inmemory DB for SQLite step by step :( – computernoob Feb 15 '19 at 10:20

0 Answers0