I think the answer would be something like this:
To store the database in memory only you can use something like:
static bool createConnection()
{
db.setDatabaseName(":memory:");
if (!db.open()) {
QMessageBox::critical(0, qApp
->tr
("Cannot open database"),
qApp->tr("Unable to establish the database connection.\n"
return false;
}
query.exec("create table table1 (someIdentifier int, someNameAssociated varchar(30))");
query.exec("create table table2 (someIdentifier int, someDateAssociated datetime)");
return true;
}
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish the database connection.\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
QSqlQuery query;
query.exec("create table table1 (someIdentifier int, someNameAssociated varchar(30))");
query.exec("create table table2 (someIdentifier int, someDateAssociated datetime)");
return true;
}
To copy to clipboard, switch view to plain text mode
While if you would like to make the database persistent accross reboots it should be saved in a file on the hard drive so you could use something only like:
static bool createConnection()
{
db.setDatabaseName("myDatabase");
if (!db.open()) {
QMessageBox::critical(0, qApp
->tr
("Cannot open database"),
qApp->tr("Unable to establish the database connection.\n"
return false;
}
query.exec("create table table1 (someIdentifier int, someNameAssociated varchar(30))");
query.exec("create table table2 (someIdentifier int, someDateAssociated datetime)");
return true;
}
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("myDatabase");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish the database connection.\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
QSqlQuery query;
query.exec("create table table1 (someIdentifier int, someNameAssociated varchar(30))");
query.exec("create table table2 (someIdentifier int, someDateAssociated datetime)");
return true;
}
To copy to clipboard, switch view to plain text mode
Note the difference between the 2 versions of createConnection:
The first one is using db.setDatabaseName(":memory:");
The second one is using db.setDatabaseName("myDatabase");
Laurent
Bookmarks