I'm building an app that is using a memory-backed database for speed (the database needs to handle at least 60 inserts/second, and I can't batch them). Periodically, I need to copy all of the records from the memory-backed database to a disk-backed one.
I assumed that I could open two connections, and then use fully-qualified names to move around the records. My code looks something like this (simplified, so it may not compile). Both databases contain behavioraldata tables with xpos,ypos, and time columns:
//Open disk-backed database
sessionDb_
= QSqlDatabase::addDatabase("QSQLITE",databaseName
);
sessionDb_.
setDatabaseName(QCoreApplication::applicationDirPath() + "/" + databaseName
+ ".sqlite");
Q_ASSERT(sessionDb_.open());
//Open memory-backed database
cacheDb_
= QSqlDatabase::addDatabase("QSQLITE",cacheDatabaseName
);
cacheDb_.setDatabaseName(":memory:");
Q_ASSERT(cacheDb_.open());
// .....Useful code removed.....
//Try to copy the behavioraldata table from the memory database to the disk database
QString queryStr
= QString("INSERT INTO %1.behavioraldata(xpos,ypos,time) " "SELECT xpos, ypos, time "
"FROM behavioraldata")
.arg(sessionDb_.connectionName());
flushQ.exec(queryStr);
//open memory-backed database
//Open disk-backed database
sessionDb_ = QSqlDatabase::addDatabase("QSQLITE",databaseName);
sessionDb_.setDatabaseName(QCoreApplication::applicationDirPath() + "/" + databaseName + ".sqlite");
Q_ASSERT(sessionDb_.open());
//Open memory-backed database
cacheDb_ = QSqlDatabase::addDatabase("QSQLITE",cacheDatabaseName);
cacheDb_.setDatabaseName(":memory:");
Q_ASSERT(cacheDb_.open());
// .....Useful code removed.....
//Try to copy the behavioraldata table from the memory database to the disk database
QSqlQuery flushQ(cacheDb_ );
QString queryStr = QString("INSERT INTO %1.behavioraldata(xpos,ypos,time) "
"SELECT xpos, ypos, time "
"FROM behavioraldata")
.arg(sessionDb_.connectionName());
flushQ.exec(queryStr);
//open memory-backed database
To copy to clipboard, switch view to plain text mode
However, when I execute this code, the query fails because it can't find the destination table. Since it wasn't entirely clear what the database "name" was, I tried using both the connection name and the file name, but neither works.
Does anyone have any idea what I'm doing wrong here? Does SQLITE/QSqlDatase not handle multiple databases? Am I screwing up the
Bookmarks