retnug
retnug

Reputation: 47

Loading sqlite database in python distribution

In setup.py I've specified package_data as the following:

packages=['londontweetgrabber'],
package_dir={'londontweetgrabber': 'londontweetgrabber'},
package_data={'londontweetgrabber': ['data/data.db']},

My directory hierarchy is roughly as follows:

londontweetgrabber/
 |
 | docs/
 | ...
 | londontweetgrabber/
    | 
    | __init__.py
    | authentication.py
    | data
        |
        | data.db
 |
 | README
 | setup.py

What I'd like to do is use sqlite3 to load the file in data.db. Trying a variation of a solution found here (How do I use data in package_data from source code?) I can verify that the data is being loaded now, but sqlite is complaining about a table not existing that I know does. I want this data.db file to be distributed with the python package I am writing as I depend on it for what I want to do.

The code I have at the moment for loading and failing to load sqlite:

import sqlite3
import pkgutil


def get_conn():
    data = pkgutil.get_data('londontweetgrabber', 'data/data.db')
    print data

    return sqlite3.connect(data)

Thanks

Upvotes: 2

Views: 851

Answers (1)

Lukas Graf
Lukas Graf

Reputation: 32650

You're actually passing data (the contents of data.db) to sqlite3.connect(). It however expects a path to your DB like /data/data.db.

See pkgutil.get_data():

The function returns a binary string that is the contents of the specified resource.

You can instead use pkg_resources.resource_filename() to build a proper filesystem path to your data.db:

import sqlite3
import pkg_resources

db_path = pkg_resouces.resource_filename('londontweetgrabber', 'data/data.db')
conn = sqlite3.connect(db_path)

Upvotes: 3

Related Questions