PDA

View Full Version : Qt SQLite same name different surname lineEdit



Rastate
26th February 2016, 10:50
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);

mydb = QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("C:/sqlite/etudiant.sqlite");

if(!mydb.open())
{
qDebug() << "Fail";
}

else
{
QSqlQueryModel *modal = new QSqlQueryModel();

qDebug() << "connected";

QSqlQuery *req = new QSqlQuery(mydb);
req->prepare("SELECT name FROM note");

req->exec();
modal->setQuery(*req);
ui->comboBox->setModel(modal);
}
}

Widget::~Widget()
{
delete ui;
}

void Widget::on_comboBox_currentIndexChanged(const QString &arg1)
{
QString name = ui->comboBox->currentText();

QSqlQuery qry;
qry.prepare("SELECT * FROM note WHERE name = '"+name+"'");

if(qry.exec())
{
while (qry.next()) {
ui->lineEdit->setText(qry.value(1).toString());
ui->lineEdit_2->setText(qry.value(2).toString());
}
}

}

CREATE TABLE note(id INTEGER PRIMARY KEY, name VARCHAR(25), surname VARCHAR(25));
INSERT INTO note VALUES(1, 'Jhon', 'Samuel');INSERT INTO note VALUES(2, 'Eddy', 'Ron');INSERT INTO note VALUES(3, 'Jhon', 'Mark');



hello everybody :), got a small problem with a tutorial was following on youtube, where when the program couldn't display ONLY last name+surname in LineEdit, even when I select the first one in comboBox, just like that:

11746
11747
11748

hope you can gimmy some help to continu my learnings in the right way, thx :) , I knew by some researches that my code will only call to display last 'Jhon' he got everytime, but how can I correct my code? some code lines will help a lot, thx again :D !!

anda_skoa
26th February 2016, 11:46
hope you can gimmy some help to continu my learnings in the right way, thx :) , I knew by some researches that my code will only call to display last 'Jhon' he got everytime, but how can I correct my code? some code lines will help a lot, thx again :D !!

You are interating over the result set, which contains more than one entry.
The while loop ends when it has the processed the last entry, which is your second Jhon.

The example simulates a search, if you want to access the data of a specific entry, address that entry directly or get a list of all entries and used the index to address the one you want.

Btw:
- you are leaking the query in the constructor, just pass the SQL string to the model
- your search query uses string concatenation instead of placeholders, this is what gets software hacked

Cheers,
_

Rastate
26th February 2016, 12:47
uhm? and how can I resolve that (even if partially) :'|?

anda_skoa
26th February 2016, 13:40
uhm? and how can I resolve that (even if partially) :'|?

Resolve what?
Not leaking the query -> already told you.
Using place holders -> QSqlQuery::prepare().
Finding the full data of the respective entry -> already suggested two options. E.g. your table has an id column. If that starts with ß, then you could use the currentIndexChanged signal that has an integer argument and use that to query.

Cheers,
_

Rastate
29th February 2016, 21:22
I'm a real begginer, can you provide me some code line please? :)

anda_skoa
29th February 2016, 21:28
Assuming the table's id starts at 0 like the combobox's index:


QSqlQuery qry;
qry.prepare("SELECT * FROM note WHERE id = :id");
qry.bindValue(":id", index);


Cheers,
_