[Qt-interest] Sqlite multithreaded application

Michele Ciampichetti cia.michele at gmail.com
Wed Oct 26 08:40:09 CEST 2011


Hello Jeremy,
thanks for your time, and sorry for my late but, I have no thread
application, but, I need to use one SQLite DB and access it form different
instance of my application called by network.

This is my connection method:


#ifndef CONNESSIONE_H

#define CONNESSIONE_H

#endif // CONNESSIONE_H

#include <QSqlDatabase>

#include <QMessageBox>

#include <QPluginLoader>

#include <QSqlDriverPlugin>

static bool createDbConnection(){

    QPluginLoader loader("qsqlite4.dll");

    QObject *plugin = loader.instance();

    if (!plugin)

        return false;

    QSqlDriverPlugin *sqlPlugin  = qobject_cast<QSqlDriverPlugin *>(plugin);

    if (!sqlPlugin ) {

        QMessageBox::warning(0, "SQL plugin", "Loading failed",
QMessageBox::Ok);

        return false;

    }

    QSqlDatabase db = QSqlDatabase::addDatabase(sqlPlugin->create("QSQLITE"));

    //QSqlDatabase db= QSqlDatabase::addDatabase("QSQLITE"); //per uso locale

    db.setDatabaseName("X:/FUSERA/RTCoppe/RTCoppe.s3db");

    //QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");

    //db.setDatabaseName("CnDBTest");

    if (!db.open()) {

        QMessageBox::critical(0, qApp->tr("Impossibile aprire il
database"), qApp->tr("Errore di connessione al database.\nControllare
la presenza di un file .s3db valido nella cartella del programma"),
QMessageBox::Cancel);

        return false;

    }

    return true;

}

Now, How do you think I may use the cloneDatabase() method? at time of
db.open() method? I've understood correctly your suggest?

Thanks again for your Help

Michele


2011/10/12 Jeremy Lainé <jeremy.laine at bolloretelecom.eu>

> Hello,
>
> I think you'll find the information you need here:
>
> http://doc.qt.nokia.com/4.7/threads-modules.html#threads-and-the-sql-module
>
> Basically you need on QSqlDatabase instance per thread. The following
> approach has worked
> nicely for me:
>
> - create a "reference" QSqlDatabase in your app's main thread
>
> - write a method which is called whenever you need to access the database,
> and clones the
> "reference" QSqlDatabase as needed for the current thread using
> QSqlDatabase::cloneDatabase()
>
> An example:
>
>
> http://opensource.bolloretelecom.eu/projects/qdjango/browser/src/QDjango.cpp#L84
>
> --
> Jeremy LAINE
> Bolloré telecom | 26, rue de Berri | F-75008 Paris
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20111026/70eba1ed/attachment.html 


More information about the Qt-interest-old mailing list