Results 1 to 7 of 7

Thread: connection QT with postgresql

  1. #1
    Join Date
    Mar 2012
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default connection QT with postgresql

    hi,
    i would like you to help me with the problem of the connection between qt and database postgresql
    i download QT 4.7.3
    and postgresql 9.1.1
    windows 7
    and i downloaded the connector odbc postgresql to create a database source in order to connect qt with postgresql, this is the code that i wrote but i had a message error
    driver not loaded
    #include <QtGui/QApplication>
    #include "mainwindow.h"
    #include<QtSql>
    #include<QtDebug>
    #include<Iostream>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
    db.setHostName("localhost");
    db.setDatabaseName("dsi64");
    db.setPassword("123");
    db.setUserName("postgres");
    if(db.open())
    {
    qDebug() <<"opened" ;
    db.close();
    }
    else
    {
    qDebug() << db.lastError().text();
    }
    MainWindow w;
    w.show();

    return a.exec();
    }
    please can you help me

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: connection QT with postgresql

    QSQLITE is surely not the driver you need to connect to a postgresql database! It is for SQLite databases...

  3. #3
    Join Date
    Mar 2012
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connection QT with postgresql

    sorry for the error ,this is the right code that i use ,
    #include <QtGui/QApplication>
    #include "mainwindow.h"
    #include<QtSql>
    #include<QtDebug>
    #include<Iostream>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
    db.setHostName("localhost");
    db.setDatabaseName("dsi64");
    db.setPassword("khadija");
    db.setUserName("postgres");
    if(db.open())
    {
    qDebug() <<"opened" ;
    db.close();

    }
    else
    {
    qDebug() << db.lastError().text();
    }
    MainWindow w;
    w.show();


    return a.exec();
    }

    and i get the error driver not loaded qsqlite is available and when i use it i get opened

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: connection QT with postgresql

    Well, you don't get it opened! You get a file created called dsi64 at your current directory. As the error says, you haven't the driver, so build it. And how about using QPSQL?

    On how to build driver see http://qt-project.org/doc/qt-4.8/sql-driver.html

  5. #5
    Join Date
    Mar 2012
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connection QT with postgresql

    well, i think the problem is with qt because when i use the same driver postgresql with java i have no problem with the connection with the database .
    i don't know if it is qt installation or the version

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: connection QT with postgresql

    The problem is not with Qt. The problem is with your understanding.

    The Qt plugin for direct access to PostgreSQL is called PSQL not QSQLITE or QODBC, just as Lykurg suggested. You should prefer the PSQL driver to access PostgreSQL database directly from Qt programs. You can use the ODBC interface to PostgreSQL if there is an ODBC driver for PostgreSQL on your machine and an ODBC data source named dsi64 with the specified credentials.

    However, since you have built neither the Qt ODBC plugin nor the Qt PostgreSQL plugin your efforts to connect to a PostreSQL database through either plugin will not get you too far, just "Driver not loaded". Please have read about the supported database types in particular the part about building the plugin for PostgreSQL.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  7. #7
    Join Date
    Mar 2012
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connection QT with postgresql

    thank you for your reply i found a solution i installed another version QT SDK 2010.05 and it works but i still have some problems such as
    for exemple
    the class QSqlQuery he says that bindValue is not a function in this class and i don't know what is the problem
    can you help me with this
    and thanks again

Similar Threads

  1. QT to Postgresql connection
    By KeithN in forum Newbie
    Replies: 28
    Last Post: 28th November 2013, 16:33
  2. Qt SDK + Symbian + PostgreSQL
    By marcovictor in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 1st March 2012, 11:17
  3. Replies: 0
    Last Post: 11th November 2011, 19:18
  4. Replies: 2
    Last Post: 11th February 2011, 17:53
  5. Replies: 1
    Last Post: 2nd April 2010, 06:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.