Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    [SOLVED] SQLite database stays closed.

    General and Desktop
    1
    2
    3388
    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.
    • R
      Ravenblack last edited by

      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 Reply Quote 0
      • R
        Ravenblack last edited by

        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 Reply Quote 0
        • First post
          Last post