I would like to use SQLite for storing some information in my plugin for QGIS.

I have a problem: if I put the db, let say test.db, among the resources as file, after compiled the resorce.qrc file, QGIS find the db, but if I insert some row in the db, QGIS does not find any more I have to compile again the file, I tried to recompile the file every time I commit something in the db, it seems to work, but is a dirty solution and I do not think I can use it on every system different from the mine, is there a better solution?


Don't put your sqlite database into a resource file, that's a really bad idea. You are converting the database into readonly binary data in your resource.py.

EDIT: Seems Qt resources are not meant just for storing strings. Use QSettings instead.

You should store the connection string (if needed) then just connect using something like sqlite3 in the standard python libs.

import sqlite3
from PyQt4.QtCore import QSettings
settings = QSettings()
connstring = settings.value("myConnectionString").toString()
conn = sqlite3.connect(connstring)

myConnectionString is defined in your settings ini/registry file as a string.

| improve this answer | |
  • Thanks for your answer, I understand that putting the sqlite database into aresource file is a very bad idea, it does not work either, my problem is that my plugin does not find the db if this is not into the resources file, so I need a way to access the db from my plugin, I found a dirty way to do it: I put a small script in python into the resources file, the script; the plugin calls the script and receives the results as it was on the command line, I do not like this way because, it works only if I launch qgis from the directory where the sqlite database is and is very slow – arpho Apr 29 '12 at 11:03
  • Can you post a code sample of what you are doing and where the db is located relative to the file calling it. – Nathan W Apr 29 '12 at 12:00
  • Hi the db was in the same directory of the plugin, this is the command I used to query the db# text= getstatusoutput(command line # in the command line I put "python myscript.py arguments" myscript query the db and does some outputs that is put in text, so I could write and read sqlite, but now I use Qsettings for all I need to save. – arpho May 1 '12 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.