Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    SQLite database

    General and Desktop
    2
    2
    1691
    Loading More Posts
    • 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.
    • V
      Verminoz last edited by

      Hello,
      I have an application that wants to read/write data from an SQLite database. I load the database but it does not really load it. I cannot read any data from it with a query and it returns a zero number of tables. The code is

      @
      // Setup database
      db = QSqlDatabase::addDatabase("QSQLITE");
      db.setHostName("/home/verminoz/mybase.db");
      db.setDatabaseName("main");

      bool ok = db.open();
      if (!ok)
      {
          QMessageBox::critical(this, tr("database error"),tr("Could not open database! Make sure your database exists!"),QMessageBox::Ok);
      }
      qDebug() << db.tables().count() << endl;
      

      @

      The driver is available and open() returns true. Any ideas?

      Thank you in advance

      Ohne Muzik ware das Leben ein Irrtum.

      1 Reply Last reply Reply Quote 0
      • L
        leon.anavi last edited by

        Hi,

        In my opinion you should remove if your database is on the same computer:
        @db.setHostName("/home/verminoz/mybase.db");@

        It seems that you are using Linux but anyway it is better to make your code platform independent. Try something like this:

        @
        db = QSqlDatabase::addDatabase("QSQLITE");
        #ifdef Q_OS_LINUX
        QString path(QDir::home().path());
        path.append(QDir::separator()).append("mybase.db");
        path = QDir::toNativeSeparators(path);
        db.setDatabaseName(path);
        #else
        db.setDatabaseName("mybase.db");
        #endif

        bool ok = db.open();
        if (!ok)
        {
        QMessageBox::critical(this, tr("database error"),tr("Could not open database! Make sure your database exists!"),QMessageBox::Ok);
        }
        qDebug() << db.tables().count() << endl;
        @

        Regards,
        Leon

        http://anavi.org/

        1 Reply Last reply Reply Quote 0
        • First post
          Last post