35

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

1

3 Answers 3

39

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

12 Comments

This looks like it works, but how efficient is it?
@Clay: like i wrote in my answer this code is more general and generic, you can use it for dumping all the database or to dump only a given table, and for how efficient it is? the code use iterator for once (less memory) and it also create all the database in a single query, which mean no up and go(less communication).
Thanks! This will help me very much. I do like the flexability of it.
Is that quadratic? I thought it was better to do query = ''.join([line for line in old_db.iterdump()])
@mouad, in C this may be quadratic, as you are building a string of length N every step. (Actually, average length is N/2, but that's still O(N)). Python may optimize this away though.
|
4

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

3 Comments

Unfortunately, I don't know any C. I did think I heard somewhere that apsw (Another Python Sqlite Wrapper) Had a backup function though. Is this true?
useful :) i just need something like this! Great thx
sqlite3_backup* C functions are not available via Python sqlite3 API. Though it should be easy to create a C extension for Python that calls them
1

An alternative, for those using python and sqlalchemy:

http://www.tylerlesmann.com/2009/apr/27/copying-databases-across-platforms-sqlalchemy/

The idea behind is to replicate the metadata from the source database to the target database and then transfer column after column.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.