Search code examples
c++qt4mingw32

My SQL query gets cut off


I have this class:

#include <QSqlError>
#include <QMessageBox>
#include <QCompleter>
#include <QFile>
#include <QTextStream>
#include <QSqlQueryModel>

#include "items.h"

Items::Items(Ui::Store *setui)
{
    form = setui;
}

void Items::getItem(int index, QString item)
{
    if(index == 1)
    {
        sqlQuery = "SELECT * FROM Items WHERE Name='" + item + "'";
        query.exec(sqlQuery);
        query.next();
        rec = query.record();

        form->itemNumManMod->setText(query.value(rec.indexOf("ID_I")).toString());
        form->purchaseManMod->setText(query.value(rec.indexOf("Purchase")).toString());
        form->saleManMod->setText(query.value(rec.indexOf("Sale")).toString());
        form->countityManMod->setText(query.value(rec.indexOf("Countity")).toString());
        form->totaleManMod->setText(query.value(rec.indexOf("Totale")).toString());
        form->minimumManMod->setText(query.value(rec.indexOf("Minimum")).toString());
        form->restManMod->setText(query.value(rec.indexOf("Rest")).toString());
        form->wsPriceManMod->setText(query.value(rec.indexOf("Vendor")).toString());
        form->wsNumManMod->setText(query.value(rec.indexOf("Min")).toString());
        QString x = idToCategory(3);
        QMessageBox::critical(0, "dd", x);
    }
    else if(index == 2)
    {
        sqlQuery = "SELECT * FROM Items WHERE Name='" + item + "'";
        query.exec(sqlQuery);
        query.next();
        rec = query.record();

        form->itemNumManDel->setText(query.value(rec.indexOf("ID_I")).toString());
        form->itemNameManDel->setText(query.value(rec.indexOf("Name")).toString());
        form->purchaseManDel->setText(query.value(rec.indexOf("Purchase")).toString());
        form->saleManDel->setText(query.value(rec.indexOf("Sale")).toString());
        form->countityManDel->setText(query.value(rec.indexOf("Countity")).toString());
        form->totaleManDel->setText(query.value(rec.indexOf("Totale")).toString());
        form->minimumManDel->setText(query.value(rec.indexOf("Minimum")).toString());
        form->restManDel->setText(query.value(rec.indexOf("Rest")).toString());
    }
    else if(index == 0)
    {
        sqlQuery = "SELECT ID_I, Sale FROM Items WHERE Name='" + item + "'";
        query.exec(sqlQuery);
        query.next();
        rec = query.record();
        form->numSaleAdd->setText(query.value(rec.indexOf("ID_I")).toString());
        form->priceSaleAdd->setText(query.value(rec.indexOf("Sale")).toString());
    }
}

QString Items::idToCategory(int id)
{
    sqlQuery = "SELECT Name FROM Category WHERE ID_C=" + id;
    query.exec(sqlQuery);
    query.next();
    rec = query.record();
    QString name = query.value(rec.indexOf("Name")).toString();
    return query.lastQuery();
}

When I call idToCategory() method, it returns this:

ECT Name FROM Items WHERE ID_C=

But it should return something like this:

SELECT Name FROM Items WHERE ID_C=3

Solution

  •  sqlQuery = "SELECT Name FROM Category WHERE ID_C=" + id;
    

    You are adding a integer value to a pointer. Try this instead:

    sqlQuery = QString("SELECT Name FROM Category WHERE ID_C=%1").arg(id);
    

    Or better, use a prepared statement, and bind the value:

    sqlQuery = "SELECT Name FROM Category WHERE ID_C = :id";
    query.prepare(sqlQuery);
    query.bindValue(":id", id);
    query.exec(); // note that you should be testing the return value of exec()
    

    PS: For your other queries, you should look at QDataWidgetMapper with a QSqlTableModel, to map once and for all the field values to each widget, rather than reassigning the value manually.