QSqlQuery throws exception in dll module
08:35 11 Sep 2015

I have 3 different projects in my solution:

  • Interops (LIB)
  • XModule(DLL, includes a QWidget) {using Interops}
  • Main App (EXE) {using Interops, XModule}

I created a logger class in the Interops project as "LogManager" and it connects to MySQL to periodically writing logs to database.

Using the "LogManager" class in "Main App" works okay, but in XModule (which is also used in "Main App") it is throwing weird exceptions during QSqlQuery execution and it isn't debuggable. I think that the dynamically linked DLL is failing to load SQL drivers of Qt but I don't know how to fix.

By the way, all projects are using the SQL Qt module. Thanks.


Two Example Functions: which succeed in MainApp but not in XModule. Throws at QString val = q.value(0).toString(); line in getLastDbLogDate() function. Also I pass pointer of LogManager instance everywhere which instanced in MainApp.

bool LogManager::connect()
{
    if(!QSqlDatabase::contains(LOG_DB_NAME))
    {
        QSqlDatabase db = QSqlDatabase::addDatabase(mDriver, LOG_DB_NAME);
        if(!db.isValid())
            return false;

        db.setHostName(mHost);
        db.setPort(mPort);
        db.setDatabaseName(mDb);
        db.setUserName(mUser);
        db.setPassword(mPw);
    
        return db.open();
    }

    return true;
}

QDateTime LogManager::getLastDbLogDate()
{
    if(!connect())
        QDateTime();

    QSqlQuery q("SELECT MAX(logTime) FROM tbl_logs", QSqlDatabase::database(LOG_DB_NAME));
    while (q.next())
    {
        QString val = q.value(0).toString();
        return QDateTime::fromString(val, LOG_DATE_SQL_FORMAT);
    }

    return QDateTime();
}

calling like everywhere :

mLogMgr->getLastDbLogDate();
c++ qt qtsql