Add 2 or more columns from MySQL to ComboBox?
-
wrote on 22 May 2017, 23:52 last edited by meepo1
Here i show my code
I want to get 2 columns from MySQL but i only can get one (maybe both) but i cannot set them in the same combobox
.
#include "Wind0.h" #include "ui_Wind0.h" #include <QSqlTableModel> #include <QMessageBox> #include <QSqlError> #include <QSqlQuery> #include <QDebug> #include <QSqlRecord> Wind0::Wind0(QWidget *parent) : QDialog(parent), ui(new Ui::Wind0) { ui->setupUi(this); mDatabase = QSqlDatabase::addDatabase("QMYSQL"); mDatabase.setHostName("localhost"); mDatabase.setDatabaseName("BD1"); mDatabase.setPort(3306); mDatabase.setUserName("root"); mDatabase.open(); } Wind0::~Wind0() { delete ui; } void Wind0::on_pushButton_clicked() { QSqlQuery query; query.prepare("SELECT Name, Group FROM Teacher"); query.exec(); query.first(); aux = query.value(0).toString(); ui->comboBox->addItem(aux); }
-
Here i show my code
I want to get 2 columns from MySQL but i only can get one (maybe both) but i cannot set them in the same combobox
.
#include "Wind0.h" #include "ui_Wind0.h" #include <QSqlTableModel> #include <QMessageBox> #include <QSqlError> #include <QSqlQuery> #include <QDebug> #include <QSqlRecord> Wind0::Wind0(QWidget *parent) : QDialog(parent), ui(new Ui::Wind0) { ui->setupUi(this); mDatabase = QSqlDatabase::addDatabase("QMYSQL"); mDatabase.setHostName("localhost"); mDatabase.setDatabaseName("BD1"); mDatabase.setPort(3306); mDatabase.setUserName("root"); mDatabase.open(); } Wind0::~Wind0() { delete ui; } void Wind0::on_pushButton_clicked() { QSqlQuery query; query.prepare("SELECT Name, Group FROM Teacher"); query.exec(); query.first(); aux = query.value(0).toString(); ui->comboBox->addItem(aux); }
@meepo1 I moved your post from "C++ GURUS" to "General and Desktop" forum as your question is not related to C++.
-
wrote on 23 May 2017, 05:35 last edited by beecksche
Hi @meepo1 ,
you only read the first record.I think you should have a look at the QSqlQuery documentation, useful informations are also http://doc.qt.io/qt-5.8/sql-sqlstatements.html, or go through the examples http://doc.qt.io/qt-5.8/examples-sql.html
I would change the code to something like that.
QSqlQuery query("SELECT Name, Group FROM Teacher"); int nameID = query.record().indexOf("Name"); int groupID = query.record().indexOf("Group"); while (query.next()) { ui->comboBox->addItem(query.value(nameID).toString()); ui->comboBox->addItem(query.value(groupID).toString()); }
-
Hi @meepo1 ,
you only read the first record.I think you should have a look at the QSqlQuery documentation, useful informations are also http://doc.qt.io/qt-5.8/sql-sqlstatements.html, or go through the examples http://doc.qt.io/qt-5.8/examples-sql.html
I would change the code to something like that.
QSqlQuery query("SELECT Name, Group FROM Teacher"); int nameID = query.record().indexOf("Name"); int groupID = query.record().indexOf("Group"); while (query.next()) { ui->comboBox->addItem(query.value(nameID).toString()); ui->comboBox->addItem(query.value(groupID).toString()); }
@beecksche I think he wants to have two columns in the combobox: one for name and one for group. So each line in the combobox consists of two columns.
-
@beecksche I think he wants to have two columns in the combobox: one for name and one for group. So each line in the combobox consists of two columns.
wrote on 23 May 2017, 06:37 last edited by@jsulm
Yeah maybe! Not clear for me in the question.
To add two different columns in one QComboBox seperately doesn't make sense for me also.Or using a QAbstractItemModel to set the items of the QComboBox could also be a solution.
-
@jsulm
Yeah maybe! Not clear for me in the question.
To add two different columns in one QComboBox seperately doesn't make sense for me also.Or using a QAbstractItemModel to set the items of the QComboBox could also be a solution.
wrote on 23 May 2017, 18:08 last edited by meepo1@beecksche @jsulm Oh so sorry it was not clear enough
When i said "columns" i meant "database attribute". I am trying to get 2 records from 2 differents attributes (Name and Group) from one table (Teacher).
I want to set the teacher's name and his Group number in my combobox.
My combobox should show:
William 01
Achilles 01
Pietro 02 -
wrote on 23 May 2017, 18:33 last edited by
@meepo1 ,
while adding into combobox, glub them together into one string or may be stringlist and use additem(0 or addItems() method.
Thanks,
-
@meepo1 ,
while adding into combobox, glub them together into one string or may be stringlist and use additem(0 or addItems() method.
Thanks,
wrote on 23 May 2017, 21:46 last edited by@Pradeep-Kumar @beecksche @jsulm I have a last question, why when i use query.first() the query starts at second row while when i do not use it, the query starts at first row?
void Wind0::on_pushButton_clicked() { QSqlQuery query; query.prepare("SELECT Name, Group FROM Teacher"); query.exec(); query.first(); while(query.next()){ int nameID = query.record().indexOf("Name"); int groupID = query.record().indexOf("Group"); QString a = query.value(nameID).toString(); QString b = query.value(groupID).toString(); n = a+" "+"("+b+")"; ui->comboBox->addItem(n); } }
-
@Pradeep-Kumar @beecksche @jsulm I have a last question, why when i use query.first() the query starts at second row while when i do not use it, the query starts at first row?
void Wind0::on_pushButton_clicked() { QSqlQuery query; query.prepare("SELECT Name, Group FROM Teacher"); query.exec(); query.first(); while(query.next()){ int nameID = query.record().indexOf("Name"); int groupID = query.record().indexOf("Group"); QString a = query.value(nameID).toString(); QString b = query.value(groupID).toString(); n = a+" "+"("+b+")"; ui->comboBox->addItem(n); } }
wrote on 24 May 2017, 03:34 last edited byHi...
Why it is retrieves second row is because, before while loop you are calling query.first() method it focuses the first row, after that in while loop again you are calling query.next() so obviously it points to second row. In your case query.first() is not required. -
wrote on 24 May 2017, 05:43 last edited by Pradeep Kumar
@meepo1
Good to hear u got answer, for the topic from other members as well , in the forum.Thanks,
1/10