Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a sqlite table from a disk database to a memory database in python? [duplicate]

How to copy a disk based sqlite table to a memory database in python? I know the schema of the table.

like image 722
Clay Avatar asked Oct 25 '10 21:10

Clay


People also ask

Can I copy SQLite database?

If your application install in android device, you can copy the sqlite DB file to the external folder with following code, then you can send the DB file by email or upload the DB file by Google drive, then you can download the file in PC.

Does SQLite supports in memory databases?

SQLite in-memory databases are databases stored entirely in memory, not on disk. Use the special data source filename :memory: to create an in-memory database. When the connection is closed, the database is deleted. When using :memory: , each connection creates its own database.


2 Answers

this code is more general but maybe it can help you:

import sqlite3  new_db = sqlite3.connect(':memory:') # create a memory database  old_db = sqlite3.connect('test.db')  query = "".join(line for line in old_db.iterdump())  # Dump old database in the new one.  new_db.executescript(query) 

EDIT : for getting your specify table you can just change in the for loop like this:

name_table = "test_table"  # name of the table that you want to get.  for line in old_db.iterdump():     if name_table in line:         query = line         break 
like image 70
mouad Avatar answered Sep 21 '22 07:09

mouad


Check out the SQLite Backup API. The example is in C, but this should show you how it's done efficiently.

like image 24
Hollister Avatar answered Sep 20 '22 07:09

Hollister