SQLite interfaces for Python and MATLAB

We briefly cover the interfaces that can be used to call an SQLite database. This is not a tutorial about the SQL language used by SQLite.


For the millionth time, we love Python.
In Python, import the package 'sqlite3'. It should be included in the core installation since Python 2.5.

import sqlite3

You can import a connection by opening a database. If the filename does not exists, an empty database is created.

conn = sqlite3.connect('my_sqlite_db.db')

To execute a query, use 'conn.query'. If the query expects a return, use fetchall() or fetchone(). If you modified the database, use conn.commit().

res = conn.execute('SELECT * FROM mytable')
data = res.fetchall()
res  = conn.execute('CREATE TABLE  t1 (t1key INTEGER)')

When you're done, close the connection. And that's it!


For additional question on SQL commands that work in SQLite, see SQLite website. For questions about the Python interface, see this website.


We present mksqlite, a tool that seems to do the job. It relies on one developer and most of the documentation is in German, but hey! Gotta love MATLAB.

To install, and it works on Ubuntu, download the source code, unzip, open MATLAB in the same directory, and call:

>> buildit
compiling release version of mksqlite...

We had some warnings, but it seem to work fine.

From what we understand, the basic commands to retrieve some information are:

>> mksqlite('open','my_sqlite_db.db')
ans =
>> res = mksqlite('SELECT artist_id FROM songs LIMIT 3')
res = 
3x1 struct array with fields:
>> mksqlite('close');

If someone understands this interface better than I do, I'd be happy to update this information! In particular, how to deal with multiple connections to different databases.