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

Problem adding new record to database,error: QSqlQuery::value: not positioned on a valid record



  • QSqlDatabase Database::getDb() const
    {
    return db;
    }

    Database::Database(QObject *parent) : QObject(parent)
    {
    db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName(settings.dbpath.get());
    db.setDatabaseName("main");
    db.setUserName("postgres");
    db.setPort(5432);
    db.setPassword("root");
    if(!db.open()){
    auto problemDialog = new InfoDialog(InfoDialog::Warning, "Baza danych", "Aplikacja napotkała na problem z połączeniem z bazą danych", nullptr, true);
    problemDialog->show();
    qDebug()<<"There is a problem with database.";
    return;
    }
    QSqlQuery query;
    query.exec("truncate active_errors");
    }

    void Database::addError(Error* error) {
    if(error==nullptr) return;
    QSqlQuery query;
    query.exec("INSERT INTO active_errors (source,value,hint,noticable,noticed,log,showinbar,clicked) VALUES ('"
    +error->source+"','"
    +error->value+"','"
    +error->hint+"',"
    +QVariant(error->noticable).toString()+","
    +QVariant(error->noticed).toString()+","
    +QVariant(error->log).toString()+","
    +QVariant(error->showInBar).toString()+","
    +QVariant(error->clicked).toString()+
    ") RETURNING id;"
    );
    if(query.next())
    error->setDbId(query.value(0).toInt());

    }
    void Database::updateSingleValueIn(const QString& dbName, const QString& valName, const QString& value, int id) {
    QSqlQuery query;
    query.exec("""UPDATE """+dbName+""" SET """+valName+"""="""+value+""" WHERE id="""+QString::number(id)+""";""");
    }

    void Database::updateErrorClicked(Error *error)
    {
    if(error->getDbId()<0) return;
    QSqlQuery query;
    query.exec("SELECT clicked FROM active_errors WHERE id="+QString::number(error->getDbId())+";");
    query.next();
    error->clicked = query.value(0).toBool();
    }
    void Database::removeMultipleErrorsByID(const QList<int> &ids)
    {
    if(ids.count()<=0) return;
    QString toDelete="";
    for(auto id: ids) {
    toDelete+=QString::number(id)+",";
    }
    toDelete.chop(1);
    QSqlQuery query;
    query.exec("DELETE FROM active_errors WHERE id IN("+toDelete+");");
    }

    int Database::addIntegral(const QString& name, const QString& ip, const QString& km, qreal posx, qreal posy, int imgId, const QStringList& zones, const QString& pass)
    {
    QSqlQuery query;
    QString zonesString = "";
    foreach(auto zone, zones) {
    zonesString+=zone+",";
    }
    zonesString.chop(1);
    query.exec("""INSERT INTO integrals (name,ip,km,posx,posy,image,zones,pass,malfunctions) VALUES ('"""+name+"""','"""+ip+"""','"""+km+"""','"""+QString::number(posx)+"""','"""+QString::number(posy)+"""','"""+QString::number(imgId)+"""','"""+zonesString+"""', '"""+pass+"""', 'EMPTY,OUT1,OUT2,OUT3,OUT4,+KPD,+EX1/2,BATT,AC,DT1,DT2,DTM,RTC,no DTR,no BATT,ext. modem,ext. model') RETURNING id;""");
    query.next();
    return query.value(0).toInt();
    }

    void Database::updateIntegral(int id, const QString& name, const QString& km, const QString& ip, int imgId, const QStringList& zones, const QString& pass)
    {
    QSqlQuery query;
    QString zonesString = "";
    foreach(auto zone, zones) {
    zonesString+=zone+",";
    }
    zonesString.chop(1);
    query.exec("""UPDATE integrals SET name='"""+name+"""', km='"""+km+"""', ip='"""+ip+"""', image='"""+QString::number(imgId)+"""', zones='"""+zonesString+"""', pass='"""+pass+"""' WHERE id="""+QString::number(id)+""";""");
    }

    int Database::addInput(Inputs &input, int connectionID, const QPointF& pos)
    {
    QSqlQuery query;
    query.exec("""INSERT INTO inputs (name,inputid,connectionid,posx,posy,type) VALUES ('"""+
    input.getName()+"""','"""+
    QString::number(input.getInputID())+"""','"""+
    QString::number(connectionID)+"""','"""+
    QString::number(pos.x())+"""','"""+
    QString::number(pos.y())+"""','"""+
    QString::number(static_cast<int>(input.getSensorType()))+"""') RETURNING id;""");
    query.next();
    return query.value(0).toInt();
    }

    void Database::removeIntegral(int id)
    {
    QSqlQuery query;
    query.exec("""DELETE FROM integrals WHERE id="""+QString::number(id)+""";""");
    query.exec("DELETE FROM inputs WHERE connectionid="+QString::number(id)+";");
    }

    QSqlQueryModel* Database::getModelByName(const QString& name, int startsEndsWith) {
    auto toRet = new QSqlQueryModel();
    auto qry = QSqlQuery(db);
    if(startsEndsWith==0) {
    qry.prepare("""select * from integrals WHERE value='"""+name+"""';""");
    } else if(startsEndsWith==1) {

    } else if(startsEndsWith>=2) {
    
    }
    qry.exec();
    toRet->setQuery(qry);
    return toRet;
    

    }

    QSqlQueryModel *Database::getModel(const QString& text)
    {
    QSqlQueryModel toRet = new QSqlQueryModel();
    QSqlQuery
    qry = new QSqlQuery(db);
    qry->prepare("""select * from integrals WHERE"""+text);
    qry->exec();
    toRet->setQuery(*qry);
    return toRet;
    }

    QSqlQueryModel *Database::getAllIntegrals()
    {
    QSqlQueryModel toRet = new QSqlQueryModel();
    QSqlQuery
    qry = new QSqlQuery(db);
    qry->prepare("select * from integrals");
    qry->exec();
    toRet->setQuery(*qry);
    return toRet;
    }

    QSqlQueryModel *Database::getAllInputs(int id)
    {
    QSqlQueryModel toRet = new QSqlQueryModel();
    QSqlQuery
    qry = new QSqlQuery(db);
    qry->prepare("select * from inputs WHERE connectionid="+QString::number(id)+";");
    qry->exec();
    toRet->setQuery(*qry);
    return toRet;
    }


  • Lifetime Qt Champion

    First please use the code tags so we can read the code and then - what should we do with all this code without a proper description? Please post only the relevant parts and properly describe your problem and what you already tried!



  • @coutKateM

    • If you are going to post code, please make the effort to use the forum's Code tags around it to make it readable.

    • Please make an effort to provide something minimal in your questions. Not a paste of your complete code, rather something cut down which illustrates the issue with reams of code for us to look through.

    • You have the error, so please indicate which line/statement it comes from if you want others to take the time to help you.

    • Not the cause of your problem, but you are keeping a member variable of type QSqlDatabase which the docs state you should not do.

    • You have left in clearly erroneous code, such as

    query.exec("""DELETE FROM integrals WHERE id="""+QString::number(id)+""";""");
    query.exec("DELETE FROM inputs WHERE connectionid="+QString::number(id)+";");
    

    Half your other SQL statements are similarly incorrect, such as using quoting incorrectly.

    • You have a long.multi-line statement commencing
    input.getName()+"""','"""+
    

    which makes no sense as a statement, doesn't the compiler warn you about this?

    And so on....


Log in to reply