[SOLVED] SQLite database stays closed.



  • 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.



  • 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;
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.