QsqlDatabase
无法打开现有文件。该文件是QT
资源的一部分。如果我根据资源使用文件地址,则会出现错误。如果我使用绝对地址,则可以正常工作。我确保文件存在并且路径正确。
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";
}
输出看起来像
":/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.