0

QsqlDatabasecannot open an existing file.The file is part of QT Resources. If I use file address as per resources, I get error. If I use absolute address, it works fine. I made sure file exists and path is correct.

const QString DRIVER("QSQLITE");
assert( QSqlDatabase::isDriverAvailable( DRIVER ) );
m_db = QSqlDatabase::addDatabase( DRIVER );

// const QString fileName = "/Users/user.name/Projects/TraiMed/TraiMed/Data/DB/Users.db

const QString fileName = ":/Databases/Data/DB/Users.db";
m_db.setDatabaseName( fileName );
qDebug() << m_db.databaseName();
QFile file( fileName );

if ( file.exists() )
    qDebug() << "File exists!";

if ( ! m_db.open() )
{
    qDebug() << "Error: connection with database failed ";
    qDebug() << m_db.lastError();
    assert( false );
}
else
{
    qDebug() << "Database: connection ok";
}

The output looks like

":/Databases/Data/DB/Users.db"
File exists!
Error: connection with database failed 
QSqlError("14", "Error opening database", "unable to open database file")
Assertion failed: (false), function DbManager, file ../DBManager.cpp, line 27.
21:40:07: The program has unexpectedly finished.
Wafeeq
  • 869
  • 1
  • 16
  • 34
  • 1
    SQLite databases could not be stored in the Qt resource system previously, as far as I know. Technically, they can but only as a read-only file, and thus, sql functions cannot use it. – tetorea Dec 21 '18 at 21:09
  • I don't think you can use a `Qt` resource for this. You may want to extract the resource and save the file to a temporary location and then use that temporary file. – drescherjm Dec 21 '18 at 21:31
  • Are all `QT` resources read-only always? – Wafeeq Dec 21 '18 at 22:23
  • They have to be because then it would be self modifying executables. However I don't think read-only is the only problem. I believe the problem is the SQLITE code does not support using a `Qt` resource. To add support for that could be a large effort for little reward. I say little reward because of the read only problem. – drescherjm Dec 21 '18 at 22:58
  • More discussion about sqlite/qt resource can be found [here](https://stackoverflow.com/questions/40127409/is-there-any-qt-sqlite-plugin-for-storing-database-in-ram-by-vfs-for-loading-da) – tunglt Dec 21 '18 at 22:58

0 Answers0