Solved Sqlite DB is making an in-memory DB instead of a full file DB even though .setDatabaseName is using a Path
-
Sqlite DB is making an in-memory DB instead of a full file DB even though .setDatabaseName is using a Path.
-
Please show what you did exactly.
If you specify a*.db
file, it should be there, as long as you don't face any errors while opening the DB connection.For the QSQLITE driver, if the database name specified does not exist, then it will create the file for you unless the QSQLITE_OPEN_READONLY option is set.
Additionally, name can be set to ":memory:" which will create a temporary database which is only available for the lifetime of the application.
(from: https://doc.qt.io/qt-5/qsqldatabase.html#setDatabaseName)
-
It didn't let me finish typing. The once every 10 mins is a bit controlling! This is a RW database.
The funny thing is I had this was working before I added in DB creation by code instead of the tool, but something changed somewhere, perhaps an environmental change. But when I use the Sqlite tools, I see it created the DB file, dataman.db, it says the DB is a "transient in-memory database". The file is created on disk, but somewhere in the bowels of libraries it is not creating it properly. It is quite befuddling.
My source is pretty simple:
bool MdDatabaseManager::connectDb()
{
bool rc = false;
bool opened = false;
bool pre_exists = getPaths().existsDb();
m_pathDb = getPaths().makeDbPath();if (!pre_exists || (pre_exists && !m_db.isValid())) { m_db = QSqlDatabase::addDatabase("QSQLITE"); // where it holds connection m_db.setDatabaseName(m_pathDb); } bool post_exists = getPaths().existsDb(); // post check if it created it if (!(opened = m_db.isOpen())) opened = m_db.open(); if (!opened) { qDebug() << "Error: connection with database failed"; } else { rc = true; qDebug() << "Database: connection ok"; if (!pre_exists && post_exists) { m_created = true; } } return rc;
-
@SocketSackett said in Sqlite DB is making an in-memory DB instead of a full file DB even though .setDatabaseName is using a Path:
if (!pre_exists || (pre_exists && !m_db.isValid()))
{
m_db = QSqlDatabase::addDatabase("QSQLITE"); // where it holds connection
m_db.setDatabaseName(m_pathDb);
}Did you make sure you enter this if block?
-
@SocketSackett said in Sqlite DB is making an in-memory DB instead of a full file DB even though .setDatabaseName is using a Path:
The once every 10 mins is a bit controlling!
Spam / Bot protection (for new users only, once your amount of posts and your rep increases, it's gone, I guess).
Also, it's not recommended to have the DB connection laying around as member of some class. If you need to get your connection, better use
QSqlDatabase::database()
. -
@jsulm : Yes: My code is exactly like that.
-
@SocketSackett
@jsulm is asking for proof that area of code gets executed. Add this line into theif
:qDebug() << pre_exists << m_pathDb;
and show the result, please.
-
@Pl45m4 : It is a RW-DB so I haven't set the QSQLITE_OPEN_READONLY at all. I'm using the file path for the DB name.
-
@Pl45m4: Thank you for the suggestion. I've applied your suggestion, but I still have the error. But I'm keeping the suggestion in my code because it makes sense. Thx.
-
@JonB : As I said you see it do it the debugger.
-
@SocketSackett DB Path : "D://Bennubio//dataman//data_manager.db" , Pre-exists: true
-
I'm not sure what all fixed this but the approach of if (!pre_exists || (pre_exists && !m_db.isValid())) is a good check to avoid duplicate connection. Before that in a close error I was getting a message that it wasn't closing on the duplicate connection. On Stack Overflow, I found the suggestion to do:
-
@SocketSackett Continuing ... I found the suggestion on Stack Overflow to use: db = QSqlDatabase(); db.removeDatabase(cnxn); after the close was a way to fix the duplicate connection close error. This is wrong. Doing this will not allow the work done to be updated. I removed that, & did a straight DB close, & it is updating once again. Checking for pre-existence of DB path with the QSqlDatabase::database.isValid() will suffice & is a good approach.