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 database

SQLite database

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.8k Views
  • 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 Offline
    V Offline
    Verminoz
    wrote on last edited by
    #1

    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
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      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
      0

      • Login

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