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 !!
#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 !!