Ok. Here it is the details. I'm using that class from the threads. So, what I actually did was:
- Use pragmas to change the default SQLite behaviour. See details here;
- Use transactions (See QSqlDatabase::transaction () and QSqlDatabase::commit()).That let me don't really lock the write to the database for a long time;
- Play with QSQLITE_BUSY_TIMEOUT.
My code sample
const static char* SESSION_NAME = "MySession";
static const char* s_db_pragma[] = {
"synchronous=OFF",
"count_changes=OFF",
"temp_store=MEMORY",
NULL
};
// Check if SQLite driver is available
{
qDebug << "SQLite 3.x driver unavailabe. Check Qt build!";
return;
}
// Note, db_path is a path to your SQLite database
m_db.setDatabaseName(db_path);
if (!m_db.isValid() || !m_db.open())
{
qDebug << "Unable to open SQLite 3.x database from " << db_path;
return;
}
// Enable SQLite pragmas
int i = 0;
do {
query.prepare(sql);
query.exec();
if (query.lastError().isValid())
{
qDebug << "Query: \"" << query.lastQuery()
<< "\"; Error: \"" << query.lastError().text() << "\"."
return;
}
} while (s_db_pragma[++i]);
// Continue from here with your queries
const static char* SESSION_NAME = "MySession";
static const char* s_db_pragma[] = {
"synchronous=OFF",
"count_changes=OFF",
"temp_store=MEMORY",
NULL
};
// Check if SQLite driver is available
if (!QSqlDatabase::isDriverAvailable("QSQLITE"))
{
qDebug << "SQLite 3.x driver unavailabe. Check Qt build!";
return;
}
// Note, db_path is a path to your SQLite database
QSqlDatabase m_db = QSqlDatabase::addDatabase("QSQLITE", SESSION_NAME);
m_db.setDatabaseName(db_path);
if (!m_db.isValid() || !m_db.open())
{
qDebug << "Unable to open SQLite 3.x database from " << db_path;
return;
}
// Enable SQLite pragmas
int i = 0;
do {
QString sql = QString("PRAGMA %1;").arg(s_db_pragma[i]);
QSqlQuery query(m_db);
query.prepare(sql);
query.exec();
if (query.lastError().isValid())
{
qDebug << "Query: \"" << query.lastQuery()
<< "\"; Error: \"" << query.lastError().text() << "\"."
return;
}
} while (s_db_pragma[++i]);
// Continue from here with your queries
To copy to clipboard, switch view to plain text mode
Best regards,
-- Tanuki
P.S. It's quite easy to use transactions. Place QSqlDatabase::transaction () before query block and use QSqlDatabase::commit() after it. Also you can use QSqlDatabase::rollback(). Since SQLite 3.x this approach is very usefull and works good.
Bookmarks