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. PostgreSQL 9.4 QT 5.5 Trouble with connection string. #[SOLVED]
Forum Update on Tuesday, May 27th 2025

PostgreSQL 9.4 QT 5.5 Trouble with connection string. #[SOLVED]

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.4k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    metron9
    wrote on 16 Aug 2015, 17:03 last edited by metron9
    #1

    I have installed PostgreSQL 9.4 and created a database called "test"
    I created a user role_name "mark" with a password "apple"
    I get the following error when I compile and run.

    Error: QSqlError("", "QPSL: Unable to connect", "fe_sendauth: no password supplied\n")

    I am totally new to PostgreSQL as well as a Noobe with QT
    I have read many posts from the web on compiling the drivers but the one I went with simply says to copy 4 of the .DLL files from the PostgreSQL install bin folder to the QT mingw bin folder.
    libeay32
    libintl.dll
    libpq.dll
    ssleay32.dll

    I did that and from the error message it looks as though the driver is working.

    Not sure what the actual ServerName is suppose to be. Looking at the PgAdmin the servers Service ID is what I used.

    Code is from an example made by VoidRealms although his example used the "QODBC" connection.

    Any advice much appreciated to get me started here.

    The program code:

    #include <QCoreApplication>
    #include <QtSql>
    #include <QtDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QString servername = "LOCALHOST\\postgresql-9.4";
        QString dbname = "test";
        QString user = "mark";
        QString pword = "apple";
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    
        db.setConnectOptions();
    
        QString dsn = QString("*DRIVER={SQL Native Client};SERVER=%1;DATABASE=%2;UID=%3;PWD=%4").arg(servername).arg(dbname).arg(user).arg(pword);
    
    
        db.setDatabaseName(dsn);
    
        if(db.open())
        {
            qDebug() << "Opened";
            db.close();
    
        }
        else
        {
            qDebug() << "Error:" << db.lastError();
        }
    
    
        return a.exec();
    }
    

    The applications.pro code

    QT       += core
    QT       += sql
    QT       -= gui
    TARGET = dbapp
    CONFIG   += console
    CONFIG   -= app_bundle
    TEMPLATE = app
    SOURCES += main.cpp
    

    I added some error output using qDebug()
    Trying to figure out myself I can see db.database returns no username.

     qDebug() << "\nError:" << db.lastError() << "\n\nConnection Name:" << db.connectionName() << "\n\nDatabase:" << db.database()
                     << "\n\nDatabase Name:" << db.databaseName();
    

    After blowing some steam off by walking 5 miles I returned to see if I could figure it out.
    The resulting code works. I created a console application.
    Baby steps.
    Don't forget to put those .DLL files mentioned above in the correct directory.

    /*
    The PostgreSQL database I created using pgAdmin
    Name "test"
    Table name "testtable"
    Column1  "id" type = bigserial
    Column2 'address' type = character varying
     */
    
    // I dont know any Russian but this video is what I watched to get me going.
    // https://www.youtube.com/watch?v=fBgJ9Azm_S0
    
    #include <QCoreApplication>
    #include <QtSql>
    #include <QtDebug>
    #include <QSqlQuery>
    #include <QtCore>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QString dbname = "test";
        QString user = "mark";
        QString pword = "apple";
        QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    
        db.setPort(5432);
        db.setHostName("localhost");
        db.setDatabaseName(dbname);
    
        // Create a SQL Query Object
        QSqlQuery query;
    
        if(db.open(user,pword))
        {
            qDebug() << "Opened";
    
            // We will delete all the data and then add two records
    
            query.exec("delete from testtable");
            qDebug() << query.lastError();
            query.exec("insert into testtable(address) values('Rice Street')");
            qDebug() << query.lastError();
            query.exec("insert into testtable(address) values('Xerxes Road')");
            qDebug() << query.lastError();
    
            // Execute a query
            if ( query.exec("SELECT  * FROM testtable"))
            {
                qDebug() << "Query Success!";
    
                // We have to call the next() method to point to the first record.
                query.next();
                // To skip backwards one record we can call the previous() method
                // query.previous();
                // The in memory column fields data are in an array named value()
                // The first column has the index value of zero not 1 we call this zero based indexing.
                // So our field "id" is value(0) and our field "address" is value(1)
                qDebug() << "First Record in Table field  = " << query.value(1).toString();
                query.next();
                qDebug() << "Second Record in Table field  = " << query.value(1).toString();
                QString tableAddress  =  query.value(1).toString();
                qDebug() << tableAddress;
    
                // Lets change some data in the second records address field
    
                // Using QTextStream we will build a query string that has the old address to be used
                // in the WHERE command. We will use the query string variable in the query.exec() command.
    
                // **NOTE**
    
                // I understand using the  QSqlQuery::bindValue() is a better way to do this.
                // That will be my next exploration. For now though I'm happy to be able to connect
                // to a postgreSQL database read and write to a table.
    
                QString queryString;
                QTextStream queryStream(&queryString);
                queryStream
                        << "UPDATE testtable SET address = '5523 Baymont Street' WHERE address ='"
                        <<  tableAddress << "'";
                qDebug() << queryString;
                query.exec(queryString);
                qDebug() << query.lastError();
                query.exec("SELECT  * FROM testtable");
                query.next();
                qDebug() << "First Record in Table field  = " << query.value(1).toString();
                query.next();
                qDebug() << "Second Record in Table field  = " << query.value(1).toString();
            }
            else
            {
                qDebug() << "Query Failed to Execute";
                qDebug() << "query.lasterror() = " << query.lastError();
            }
            db.close();
         }
        else
        {
            qDebug() << "\nError:" << db.lastError() << "\n\nConnection Name:" << db.connectionName() << "\n\nDatabase:" << db.database()
                     << "\n\nDatabase Name:" << db.databaseName();
        }
        return a.exec();
    }
    

    The applications.pro file

    QT       += core
    QT       += sql
    QT       -= gui
    TARGET = dbapp
    CONFIG   += console
    CONFIG   -= app_bundle
    TEMPLATE = app
    SOURCES += main.cpp
    
    1 Reply Last reply
    0

    1/1

    16 Aug 2015, 17:03

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved