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

In another verion of program I can't add a new record into the database, I have an error: QSqlQuery::value: not positioned on a valid recor



  • #include "database.h"
    #include <utils/converter.h>
    #include <dialogs/infodialog.h>
    #include <mainmenu.h>
    #include <QTimer>
    #include <QTime>
    QSqlDatabase Database::getDb() const
    {
    return db;
    }

    Database::Database(QObject *parent) : QObject(parent)
    {
    db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName(settings.dbpath.get());
    db.setDatabaseName("postgres");
    db.setUserName("postgres");
    db.setPort(5432);
    db.setPassword("zalfon19");
    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.";
    }

    qDebug()<<"There is no problem with database.";
    

    }

    int Database::addIntegral(const QString& name, const QString& ip, qreal posx, qreal posy, int imgId, const QStringList& zones, const QString& pass)
    {
    QSqlQuery querry;
    QString zonesString = "";
    foreach(auto zone, zones) {
    zonesString+=zone+",";
    }
    zonesString.chop(1);
    querry.exec("""INSERT INTO integrals (name,ip,posx,posy,image,zones,pass) VALUES ('"""+name+"""','"""+ip+"""','"""+QString::number(posx)+"""','"""+QString::number(posy)+"""','"""+QString::number(imgId)+"""','"""+zonesString+"""', '"""+pass+"""') RETURNING id;""");
    querry.next();
    return querry.value(0).toInt();
    }

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

    void Database::updateInput(Inputs& input) {
    QSqlQuery querry;
    querry.exec("""UPDATE inputs SET name='"""+
    input.getName()+"""', inputid="""+
    QString::number(input.getInputID())+""", type="""+
    QString::number(static_cast<int>(input.getSensorType()))+""" WHERE id="""+QString::number(input.getDbID())+""";""");
    }

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

    int Database::addInput(Inputs &input, int connectionID, const QPointF& pos)
    {
    QSqlQuery querry;
    querry.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;""");
    querry.next();
    return querry.value(0).toInt();
    }

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

    QSqlQueryModel* Database::getModelByName(const QString& name, int startsEndsWith) {
    auto toRet = new QSqlQueryModel();
    auto qry = new 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::getAllErrors()
    {
    QSqlQueryModel toRet = new QSqlQueryModel();
    QSqlQuery
    qry = new QSqlQuery(db);
    qry->prepare("select * from public.active_errors");
    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;
    }

    QSqlQueryModel *Database::getUpdate(int id) {
    QSqlQueryModel toRet = new QSqlQueryModel();
    QSqlQuery
    qry = new QSqlQuery(db);
    qry->prepare("select alarm_list, armed_list, malf_list, conn, input_states from integrals WHERE id="+QString::number(id)+";");
    qry->exec();
    toRet->setQuery(*qry);
    return toRet;
    }

    int Database::getVersion(int id) {
    QSqlQueryModel toRet = new QSqlQueryModel();
    QSqlQuery
    qry = new QSqlQuery(db);
    qry->prepare("select ver from integrals WHERE id="+QString::number(id)+";");
    qry->exec();
    toRet->setQuery(*qry);
    return toRet->record(0).value(0).toInt();
    }

    The other version of program which is not working:

    #include "database.h"
    #include <utils/converter.h>
    #include <dialogs/infodialog.h>
    #include <mainmenu.h>
    #include <QTimer>
    #include <QTime>
    QSqlDatabase Database::getDb() const
    {
    return db;
    }

    Database::Database(QObject *parent) : QObject(parent)
    {
    db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName(settings.dbpath.get());
    db.setDatabaseName("postgres");
    db.setUserName("postgres");
    db.setPort(5432);
    db.setPassword("zalfon19");
    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;
    }
    qDebug()<<"There is no problem with database.";

    QSqlQuery querry;
    querry.exec("truncate active_errors");
    

    // querry.exec("truncate active_errors");
    // querry.exec();

    }

    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 querry;
    QString zonesString = "";
    foreach(auto zone, zones) {
    zonesString+=zone+",";
    }
    zonesString.chop(1);
    querry.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;""");
    querry.next();
    return querry.value(0).toInt();
    }

    void Database::addError(Error* error) {
    if(error==nullptr) return;
    QSqlQuery querry;
    querry.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(querry.next())
    error->setDbId(querry.value(0).toInt());

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

    void Database::updateErrorClicked(Error *error)
    {
    if(error->getDbId()<0) return;
    QSqlQuery querry;
    querry.exec("SELECT clicked FROM active_errors WHERE id="+QString::number(error->getDbId())+";");
    querry.next();
    error->clicked = querry.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 querry;
    querry.exec("DELETE FROM active_errors WHERE id IN("+toDelete+");");
    }

    void Database::updateIntegral(int id, const QString& name, const QString& km, const QString& ip, int imgId, const QStringList& zones, const QString& pass)
    {
    QSqlQuery querry;
    QString zonesString = "";
    foreach(auto zone, zones) {
    zonesString+=zone+",";
    }
    zonesString.chop(1);
    querry.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 querry;
    querry.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;""");
    querry.next();
    return querry.value(0).toInt();
    }

    void Database::removeIntegral(int id)
    {
    QSqlQuery querry;
    querry.exec("""DELETE FROM integrals WHERE id="""+QString::number(id)+""";""");
    querry.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;
    }
    void Database::updateInput(Inputs& input) {
    QSqlQuery querry;
    querry.exec("""UPDATE inputs SET name='"""+
    input.getName()+"""', inputid="""+
    QString::number(input.getInputID())+""", type="""+
    QString::number(static_cast<int>(input.getSensorType()))+""" WHERE id="""+QString::number(input.getDbID())+""";""");
    }

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

    QSqlQueryModel *Database::getUpdate(int id) {
    QSqlQueryModel toRet = new QSqlQueryModel();
    QSqlQuery
    qry = new QSqlQuery(db);
    qry->prepare("select alarm_list, armed_list, malf_list, conn, input_states from integrals WHERE id="+QString::number(id)+";");
    qry->exec();
    toRet->setQuery(*qry);
    return toRet;
    }

    int Database::getVersion(int id) {
    QSqlQueryModel toRet = new QSqlQueryModel();
    QSqlQuery
    qry = new QSqlQuery(db);
    qry->prepare("select ver from integrals WHERE id="+QString::number(id)+";");
    qry->exec();
    toRet->setQuery(*qry);
    return toRet->record(0).value(0).toInt();
    }



  • @coutKateM
    You have just raised this as another topic at https://forum.qt.io/topic/123130/problem-adding-new-record-to-database-error-qsqlquery-value-not-positioned-on-a-valid-record, and not even waited for answers there.

    Please do not duplicate questions like this. It wastes the time of those who try to reply, and it is a courtesy to them at least not to do this....


  • Lifetime Qt Champion

    Dupe - closed.


Log in to reply