[GIS] How to use SQLite db with QGIS plugin

pluginspythonqgissqlite

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?

Best Answer

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.