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] QtSql connection
QtWS25 Last Chance

[SOLVED] QtSql connection

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.1k 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 create a connection to a database and insert/delete/make queries to the database. I know SQL relatively well but I cannot seem to wrap my head around it in Qt. I used to program in Delphi.

    This is my code so far:

    @QSqlDatabase db;
    db.addDatabase("QSQLITE");
    db.setHostName( "localhost" ); //I don't know if i should include this the database is in the same directory as my program
    db.setDatabaseName( "Xmato.odb" );
    db.setUserName( "" ); //There is no username
    db.setPassword( "" ); //There is no password
    db.open();
    db.prepare("SELECT * FROM Members");
    db.exec();
    @
    I have added this to my .pro file:

    @QT += sql;@

    And added this to my main file:

    @#include <QtSql>@

    When I run this code I get the error:

    @QSqlQuery::prepare: database not open@

    db.lastError().text() returns:

    @Driver not loaded@

    Which driver sould I use and what are the drivers?

    Any ideas will me much appreciated.

    P.S.: I use c++ on Linux Ubuntu 12.04 and use Qt Creator and used LibreOffice Base to create my database.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      djmig
      wrote on last edited by
      #2

      You should start off with Qt examples. Install with:
      @sudo apt-get install qt4-demos@

      Then you can find SQL examples in:
      @/usr/lib/qt4/examples/sql@

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

        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