How can I create and use several connection to sql db in a different threads in the program using Qt?
// general worker init slot
DbWorker::init()
{
this->db = QSqlDatabase::addDatabase("QSQLITE", dbName);
db.setDatabaseName(dbPath);
db.open();
}
when in your main class or wherever you have something like:
DbWorker w1 = new DbWorker;
w1.setDbName("mem_db");
w1.setDbPath(":memory:");
QThread* t1 = new QThread(this);
w1->moveToThread(t1);
connect(t1, SIGNAL(started()), w1, SLOT(init()));
t1->start();
DbWorker w2 = new DbWorker;
w1.setDbName("file_db");
w1.setDbPath("~/usr/foo.db");
QThread* t2 = new QThread(this);
w1->moveToThread(t1);
connect(t2, SIGNAL(started()), w2, SLOT(init()));
t1->start();
so you have your memory connection in thread1 and the file connection in thread2. the only thing to manage is to fetch data to the gui thread if it is a gui application