multiple in memory sqlite databases with Qt
What is the CORRECT syntax for opening multiple SQLite In Memory databases with Qt? The example found here:
http://www.sqlite.org/inmemorydb.html
rc = sqlite3_open("file:memdb1?mode=memory&cache=shared ", &db);
for a name string does not work.
Thanks,
Roland
Re: multiple in memory sqlite databases with Qt
You do it exactly the same way you handle multiple database connections to other databases.
Code:
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QStringList>
#include <QDebug>
int main(int argc, char **argv)
{
db1.setDatabaseName(":memory:");
if (db1.open()) {
qDebug() << "db1 open";
QSqlQuery qry
("CREATE TABLE test1 (a integer)", db1
);
qDebug() << "Tables in db1" << db1.tables();
}
db2.setDatabaseName(":memory:");
if (db2.open()) {
qDebug() << "db2 open";
QSqlQuery qry
("CREATE TABLE test2 (a integer)", db2
);
qDebug() << "Tables in db2" << db2.tables();
}
return 0;
}
Re: multiple in memory sqlite databases with Qt
I will try it, but according to the SQLite link I posted :memory: only opens a single database, i.e. the second request is going to open the SAME database on a different connection. I need to open multiple memory databases on different connections. Each database has completely different content.
Re: multiple in memory sqlite databases with Qt
I you bothered to run my example code you would find that they are two, independent in-memory databases with different tables. This is exactly as described in the Sqlite page you linked:
Quote:
Every :memory: database is distinct from every other. So, opening two database connections each with the filename ":memory:" will create two independent in-memory databases.