Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPSQL: Unable to create query



  • Hi!
    I I get error: QPSQL: "42601", "QPSQL: Unable to create query".
    How do I fix this?

    void DataBase::openDB()
    {
        dataBase = QSqlDatabase::addDatabase("QPSQL");
        dataBase.setDatabaseName("db_taxi_vik_park");
        //ResumeDB.setDatabaseName("1");
        dataBase.setUserName("postgres");
        dataBase.setPassword("1qaz");
        //ResumeDB.setHostName("127.0.0.1");
        dataBase.setPort(5433);//*/
        if (dataBase.open()) {
            qDebug()<<"db is opened \r\n";}
        else {
            qDebug()<<"db not opened \r\n";}
    }
    
    QVector<QMap<QString, QString> > DataBase::getAllAccauntWithoutPhoto()
    {
        QVector<QMap<QString, QString> > resultVector;
        QMap<QString, QString> valueMap;
        QSqlQuery query;
        query.prepare("SELECT "
                      "id, "
                      "name, "
                      "email, "
                      "login, "
                      "password, "
                      "date_create, "
                      "date_block, "
                      "id_key_device, "
                      "number_of_downloaded_games, "
                      "blocked_bool, "
                      "comment "
                      " FROM public.account "
                      " ;");
    
        if (!query.exec()) {qDebug()<<"not get data from db \r\n"<<query.lastError()<<"\r\n";}
        else {
            qDebug()<<"get data from db \r\n";
            query.first();
    
            valueMap["id"] = query.value(0).toString();
            valueMap["name"] = query.value(1).toString();
            valueMap["email"] = query.value(2).toString();
            valueMap["login"] = query.value(3).toString();
            valueMap["password"] = query.value(4).toString();
            valueMap["date_create"] = query.value(5).toString();
            valueMap["date_block"] = query.value(6).toString();
            valueMap["id_key_device"] = query.value(7).toString();
            valueMap["number_of_downloaded_games"] = query.value(8).toString();
            valueMap["blocked_bool"] = query.value(9).toString();
            valueMap["comment"] = query.value(10).toString();
    
            resultVector.append(valueMap);
    
            while (query.next()) {
                valueMap["id"] = query.value(0).toString();
                valueMap["name"] = query.value(1).toString();
                valueMap["email"] = query.value(2).toString();
                valueMap["login"] = query.value(3).toString();
                valueMap["password"] = query.value(4).toString();
                valueMap["date_create"] = query.value(5).toString();
                valueMap["date_block"] = query.value(6).toString();
                valueMap["id_key_device"] = query.value(7).toString();
                valueMap["number_of_downloaded_games"] = query.value(8).toString();
                valueMap["blocked_bool"] = query.value(9).toString();
                valueMap["comment"] = query.value(10).toString();
                resultVector.append(valueMap);
            }
        }
        return resultVector;
    }
    

  • Lifetime Qt Champion

    By looking what this error means: https://www.postgresql.org/docs/10/errcodes-appendix.html

    Try to run your query directly with psql to see where exactly the error is.
    Also there is no need to use prepare() here since you don't bind any values.



  • @Christian-Ehrlicher I tried making a query in psql and the query works.
    I also tried to make this request

        QSqlQuery query("select * from public.account");
        QSqlError error = query.lastError();
        if (error.type() == QSqlError::NoError) {
            while(query.next())
            {
                qDebug() << query.value(0).toString();
            }
        }
        else {
            qDebug() << error.text();
        }
    

    But I got the error of not having a table, whereas the table is exactly there.


  • Lifetime Qt Champion

    The table is 'account', the schema public - try without schema



  • @Christian-Ehrlicher Can there be a problem with a bad driver?



  • @Christian-Ehrlicher it is too not work:

    QSqlQuery query("SELECT *  FROM account ", dataBase);
    
    
        if (!query.exec()) {
            qDebug()<<"not get data from db \r\n"<<query.lastError()<<"\r\n";
        qDebug()<<query.lastQuery();
        }
        else {
            qDebug()<<"get data from db \r\n";
            query.first();
    
    
    
        }
        return resultVector;
    


  • I use psql 12 in the windows 7 64


  • Lifetime Qt Champion

    So where do you get the error? during query.exec()? And what error? The same as in the first post?



  • @Christian-Ehrlicher said in QPSQL: Unable to create query:

    query.exec()

    Yes, in query.exec(). The same error. Looks like it's the driver. How do I find out which version of the driver is designed for?



  • Do you connect PSQL to the project and it works normally?


  • Lifetime Qt Champion

    @Mikeeeeee I'm using psql since ages with Qt. Imo it's a problem with your psql installation. Simply test it with a sqlite database. Also you should take a look what others did to solve the psql 42601 error but that's not Qt related



  • Before the update, I had PSQL working, after the update, new drivers were added and it stopped working.


  • Lifetime Qt Champion

    Thenswitch back to the old PostgreSQL - or do you really need some new features in PostgreSQL 12 (btw: psql 12 is supported since Qt5.14 and works fine here)



  • Sorry, I'm in a connection indicated is not the base, my stupid mistake.


Log in to reply