Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SQLite adding Table query fails
Qt 6.11 is out! See what's new in the release blog

SQLite adding Table query fails

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 563 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by
    #1

    I have the following code:

        auto db = QSqlDatabase::addDatabase("QSQLITE", "Connection");
        db.setDatabaseName("test.db");
        if (!db.open()) {
            qDebug("Error occurred opening the database.");
            qDebug("%s.", qPrintable(db.lastError().text()));
            return;
        }
    
        if(!db.isDriverAvailable("QSQLITE"))
        {
            qDebug("Driver not available");
            return;
        }
    
        // Insert table.
        QSqlQuery query(db);
        qDebug() << query.prepare("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, text TEXT)");
        if (!query.exec()) {
            qDebug("Error occurred creating table.");
            qDebug() << db.lastError().text();
            return;
        }
    

    When I try to insert a table the query fails and this part is excuted:

        if (!query.exec()) {
            qDebug("Error occurred creating table.");
            qDebug() << db.lastError().text();
            return;
        }
    

    However theres no error code stating what went wrong. Console output is simply this:

    true
    Error occurred creating table.
    " "
    

    So what could be the reason for a query.excec() to fail?

    As a side note I run Qt under KDE Neon Linux. Could it be a OS issue?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sandro4912
      wrote on last edited by
      #2

      I found the answer myself. In linux you have to store the database under home according to http://qt.shoutwiki.com/wiki/Creating_an_SQLite_database_in_Qt

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

      That solved my issue now it works

      I wonder whats the reason.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Your executable might be in a read-only folder and since you are using a relative path, it will try to create the file while it can not.

        A better solution is to use QStandardPaths::writableLocation so you can get a suitable folder cross-platform.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • S Offline
          S Offline
          sandro4912
          wrote on last edited by
          #4

          Thanks for the hint. I changed it like this

          QString path(QStandardPaths::writableLocation(
              QStandardPaths::StandardLocation::DesktopLocation));
          path.append(QDir::separator()).append("test.sqlite");
          path = QDir::toNativeSeparators(path);
          db.setDatabaseName(path);
          

          it seems to work

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved