0

I am unable to understand what I am doing wrong here. I am using Qt 5.7.1 and the code is as follows:

    QString filePath = QCoreApplication::applicationDirPath();
    QString dbPath = QDir(filePath).absoluteFilePath("../../../Database");

    m_db = QSqlDatabase::addDatabase("QSQLITE", "user_connection");
    m_db.setDatabaseName(dbPath + "/Sensor_Objects.db");

    qDebug() << filePath << " & " << dbPath;

    if (!m_db.open())
       qDebug() << "Database Error: " + m_db.lastError().text();
    else
    {
       qDebug() << "Database: connection ok";
        createDatabase("Sensor_Objects");
        m_db.close();
    }

qDebug() is printing the paths to the directories correctly and yet m_db.open() fails with the error "Database Error: out of memory Error opening database".

user2522981
  • 163
  • 3
  • 18
  • Use a debugger. Set a breakpoint on `sqlite3_open`, `sqlite3_open_v2` etc. Check that they are called with the correct arguments. On Linux, using `strace` & `ltrace` might help too. BTW, the bug might be outside of the lines you are showing. – Basile Starynkevitch Dec 30 '16 at 08:39
  • The thing is..the same code worked perfectly when I was using Qt 5.6. Could it be a problem with the 5.7 driver? – user2522981 Dec 30 '16 at 09:13
  • Are you sure your program don't have [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior) - perhaps somewhere else? And you don't show all your source code. – Basile Starynkevitch Dec 30 '16 at 09:24
  • Check if you really have Sensor_Objects.db at specified path. Also check if you have rights to read and write to that path and DB. – Max Pashkov Dec 30 '16 at 09:35
  • It started working! I am not really sure how though..I didn,t change anything in the code that deals directly with the database. As @BasileStarynkevitch said..the error was probably comming from elsewhere. Anyway, thanks for all the help! – user2522981 Dec 30 '16 at 11:53
  • Just as addition due to internals of sqlite3 there is not that much sense to analyse errorText after unsuccessful open, as it calls sqlite3_errmsg on invalid handler, so you see "out of memory" however it can be completely other reason. Try to use driverText() instead. – evilruff Dec 30 '16 at 21:12

1 Answers1

0

Probably the point is dbpath,try with

m_db.setDatabaseName(dbPath.toLatin1() + "/Sensor_Objects.db");