1

In my application I am using a QSqlDatabase to store some information. I did built it successfully with the following command (no errors even with verbose=3):

macdeployqt AppName.app/ -dmg -qmldir=~/dev/AppName/qml

Before that I successfully installed mysql55 using port

sudo port install mysql55

If I try to run my application it does not crash, but it also does not anything which involves database access.

This is my code for opening a database etc:

#include <QSqlQuery>
#include <QSqlDriver>

const QString DATABASE_NAME = "com.company.program.db";

MyClass::MyClass(QObject *parent) : QObject(parent) {
    mDB = QSqlDatabase::addDatabase("QSQLITE");

    #ifdef Q_OS_LINUX
    QString path(QDir::home().path());
    path.append(QDir::separator()).append(DATABASE_NAME);
    path = QDir::toNativeSeparators(path);
    mDB.setDatabaseName(path);           // NOTE: We have to store database file into user home folder in Linux
    #else
    mDB.setDatabaseName(DATABASE_NAME);  // NOTE: File exists in the application private folder, in Symbian Qt implementation
    #endif

    bool notOpened = !mDB.open();

    QMessageBox::critical(0, "MyClass", mDB.lastError().databaseText()); // always is 'out of memory'

    if (notOpened) emit failedToOpen();
    else {
        // some initialization
    }
}

MyClass::~MyClass() {
    mDB.close();
}

Has anyone experienced the same issues / a fix for this one?

Note: I am using Mac OS X 10.9 and Qt5.3.2

Niklas
  • 23,674
  • 33
  • 131
  • 170
  • after open is failed, try to get and check the error message by calling mDB->lastError()->databaseText() – Max Go Oct 02 '14 at 08:46
  • Strange thing is it does not emit the signal `failedToOpen` – Niklas Oct 02 '14 at 09:18
  • Get the error message just after if (!mDB.open()) in the code – Max Go Oct 02 '14 at 09:20
  • @N1ghtLight I get 'out of memory' exception. How can this happen? I have got only 2 tables, which do not have any entries at the beginning. – Niklas Oct 06 '14 at 07:06
  • Niklas, search engine should be your best friend ... http://stackoverflow.com/a/18325616/2266412 – Max Go Oct 06 '14 at 07:10
  • Well I just open it in one place, which is the one you see above in my code. – Niklas Oct 06 '14 at 07:51
  • please update your code with place from where you retrieve this sql error. – Max Go Oct 06 '14 at 07:53
  • it looks like that you give the wrong path to your database file, you can check existence of the file with static QFile::exists(const QString & fileName) ... try to check the file existence before you set database name. – Max Go Oct 06 '14 at 08:03
  • I updated my question with the constant (DATABASE_NAME). I mainly took this code from [here](http://developer.nokia.com/community/wiki/Creating_an_SQLite_database_in_Qt). Is there anything wrong with this? – Niklas Oct 06 '14 at 08:15
  • As I wrote above, try to verify is your database file exists by provided path or not. Code looks good, but your database file should be placed in the working directory of the application. – Max Go Oct 06 '14 at 08:21
  • @N1ghtLight thanks for your help. I managed to fix the issue. – Niklas Oct 06 '14 at 11:01

2 Answers2

0

The Database Drivers are loaded at run-time. You won't get any database-config related errors during the compilation process, if the db-driver paths' are not correct. The paths' can't even be incorrect, as you build on a machine where Qt is installed.

On Windows this occurs often, when the exe can't access files in plugins\sqldrivers. Is there something like Linux's 'strace' on MAC? If yes, you could trace, in which paths your exe attempts to find driver plugins.

I always copy the entire plugin directory in installer and deploy it in the root-directory of the executable e.g. C:\Program Files (x86)\\plugins

Hope it helps.

Valentin H
  • 7,240
  • 12
  • 61
  • 111
0

I managed to fix the out of memory error by executing this code:

#ifdef Q_OS_MAC
    QString databaseName = QApplication::applicationDirPath().append("/").append(DATABASE_NAME);
    mDB.setDatabaseName(databaseName);
#endif
Niklas
  • 23,674
  • 33
  • 131
  • 170