Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite3 connection from StringIO (Python)

I am wondering if anyone knows a way to generate a connection to a SQLite database in python from a StringIO object.

I have a compressed SQLite3 database file and I would like to decompress it using the gzip library and then connect to it without first making a temp file.

I've looked into the slqite3 library source, but it looks like filename gets passed all the way through into the C code. Are there any other SQLite3 connection libraries that you could use a file ID for? Or is there some why I can trick the builtin sqlite3 library into thinking that my StringIO (or some other object type) is an actual file?

like image 365
horriblyUnpythonic Avatar asked Nov 19 '13 23:11

horriblyUnpythonic


People also ask

How do I connect sqlite3 to Python?

Connect To Database#!/usr/bin/python import sqlite3 conn = sqlite3. connect('test. db') print "Opened database successfully"; Here, you can also supply database name as the special name :memory: to create a database in RAM.

How fetch data from sqlite3 database in Python?

SQLite Python: Querying Data First, establish a connection to the SQLite database by creating a Connection object. Next, create a Cursor object using the cursor method of the Connection object. Then, execute a SELECT statement. After that, call the fetchall() method of the cursor object to fetch the data.

Does SQLAlchemy work with sqlite3?

The great thing about SQLAlchemy is that it supports all popular database systems, including SQLite3, MySQL, PostgreSQL, Oracle, Microsoft SQL Server, etc.


1 Answers

The Python sqlite3 module cannot open a database from a file number, and even so, using StringIO will not give you a file number (since it does not open a file, it just emulates the Python file object).

You can use the :memory: special file name to avoid writing a file to disk, then later write it to disk once you are done with it. This will also make sure the file is optimized for size, and you can opt not to write e.g. indexes if size is really a big issue.

like image 122
Krumelur Avatar answered Sep 23 '22 23:09

Krumelur