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. [SOLVED] SQLite database stays closed.

[SOLVED] SQLite database stays closed.

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 3.5k 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.
  • R Offline
    R Offline
    Ravenblack
    wrote on last edited by
    #1

    I am trying to work with databases in Qt and I cant seem to find the problem in my code.

    @
    string currDir = get_current_dir_name();

    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName( QString::fromStdString( currDir + "\\XmatoData.sqlite" ) );
    
    if (!db.open())
    {
        QMessageBox msgBox;
        msgBox.setWindowTitle( "Xmato Database Error" );
        msgBox.setInformativeText( "Database is not connected. Error: " + db.lastError().text()  );
        msgBox.setWindowModality( Qt::ApplicationModal );
        msgBox.setStandardButtons( QMessageBox::Ok );
        msgBox.exec();
    }
    else
    {
        QMessageBox msgBox;
        msgBox.setWindowTitle( "Xmato Success" );
        msgBox.setInformativeText( "Database is connected." );
        msgBox.setWindowModality( Qt::ApplicationModal );
        msgBox.setStandardButtons( QMessageBox::Ok );
        msgBox.exec();
    }
    

    @

    That code gives no errors...

    Then when I prepare a quary:
    @
    if( db.isOpen() )
    {
    query.prepare( QString::fromStdString( _quary )
    }
    @

    I get a runtime error that says:
    @
    QSqlQuery::prepare: database not open
    @

    _quary is a valid quary.

    Any ideas? Thanks.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Ravenblack
      wrote on last edited by
      #2

      Okay, I found the solution..

      1. I uninstalled Qt
      2. Downloaded the tarball (.tar.gz file containing the source code for Qt (Windows users use the .zip file)).
      3. Configure with this command:
        @
        ./configure -plugin-sql-sqlite
        @

      or
      @
      ./configure -qt-sql-sqlite
      @

      If it gives you some shit about qtwebkits use just add the following to the configure command:
      @
      -skip qtwebkit
      @

      1. make and install with this commands:
        @
        make
        @

      after a few frieking hours, then
      @
      sudo make install
      @

      1. Now this is very important:
        when you declare QSqlQuery, declare it after you added the driver, i.e.:
        @
        QSqlDatabase db;
        db = QSqlDatabase::addDatabase( "QSQLITE" );
        if( db.open() )
        {
        QSqlQuery query; //NOTE THIS IS AFTER THE addDatabase("QSQLITE") FUNCTION!
        }
        @

      If for some reason you need the query to be declared earlier (like in a class) do this:
      @
      QSqlDatabase db;
      QSqlQuery query;

      //This is in another part of the code
      db = QSqlDatabase::addDatabase( "QSQLITE" );
      if( db.open() )
      {
      QSqlQuery tmp;
      query = tmp;
      }
      @

      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