Re: QSqlQuery and QLineEdit
at first, you have only one column (according to your query text)
second, you set position only on first record (query.next())
and then try to read data from non-existed columns (you have only one column!)
so, try this code
Code:
....
while (query.next()) {
QString data
= query.
value(0).
toString();
//first column == nome_prod ....
}
....
read for detailes QSqlQuery::next
Re: QSqlQuery and QLineEdit
The result of this query is only one line and one column...
The result is DISCO1
I just want to see this word in the QLineEdit, thats why I didn't use the while loop.
Why the result doesn't show? What signal/slot should I use?
Signal->clicked()
Slot-> ???
Renan
Re: QSqlQuery and QLineEdit
if you need only one result, then
Code:
query.next();
QString res
= query.
valut(0).
toString();
Re: QSqlQuery and QLineEdit
I don't know what "record" is in your case, but I'd guess it's an invalid QSqlRecord with a count of 0, thus your loop never gets executed. I'd suggest doing it the way spirit said.
Re: QSqlQuery and QLineEdit
New code and still no result:
Code:
void testeMYSQL2::on_pushButton_clicked()
{
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
db.open();
/* if (!db.open()){
QMessageBox::critical(0, tr("Error"),
QString("The error:\n%1").arg(db.lastError().text()));
}
else{
QMessageBox::information(0, tr("OK"), QString("The is NO error\n"));
}
*/
query.exec("select nome_prod from produtos where cod_prod = 1");
query.next();
QString name
= query.
value(0).
toString();
nameEdit->setText(name);
}
Re: QSqlQuery and QLineEdit
Code:
void testeMYSQL2::on_pushButton_clicked()
{
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << query.lastError().text();
return;
}
if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
qDebug() << query.lastError().text();
return;
}
if (!query.next()) {
qDebug() << query.lastError().text();
return;
}
QString name
= query.
value(0).
toString();
nameEdit->setText(name);
}
run this code and show console messages.
Re: QSqlQuery and QLineEdit
gul@gul-laptop:~/qt/testemysql2$ ./testemysql2
QSqlQuery::exec: database not open
"Driver not loaded Driver not loaded"
=-=-=-=-=-=-==-=-=-=-
It doesn't show any messages:
gul@gul-laptop:~/qt/testemysql2$ ./testemysql2
Re: QSqlQuery and QLineEdit
did you have compiled plugin or qt with mysql driver?
Re: QSqlQuery and QLineEdit
It is working.
Code:
#
void testeMYSQL2::on_pushButton_clicked()
{
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << query.lastError().text();
return;
}
if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
qDebug() << query.lastError().text();
return;
....
}
this line, the fisrt one: qDebug() << query.lastError().text();
should be: qDebug() << db.lastError().text();
to compile.
because it is testing its connection
never mind., but it doesn't show anything to me.
Re: QSqlQuery and QLineEdit
what can I do to get those lastError() ???
Renan
Re: QSqlQuery and QLineEdit
does qDebug() << db.lastError().text(); return nothing?
if QSqlDatabase::lastError doesn't return anything then everything is ok.
Re: QSqlQuery and QLineEdit
ok, but
if everything is ok, why QLineEdit doesn't show the result?
what about Signals and Slots?
Renan
Re: QSqlQuery and QLineEdit
Quote:
Originally Posted by
GuL
ok, but
if everything is ok, why QLineEdit doesn't show the result?
you showed error message on console "driver not loaded". doesn't this error appear anymore?
Quote:
Originally Posted by
GuL
what about Signals and Slots?
what slots and signals?
Re: QSqlQuery and QLineEdit
Quote:
Quote:
Originally Posted by GuL View Post
ok, but
if everything is ok, why QLineEdit doesn't show the result?
you showed error message on console "driver not loaded". doesn't this error appear anymore?
No, it doesnt. I have changed your code in order to compile and that error happened. Than I changed it again and it worked!
Quote:
Quote:
Originally Posted by GuL View Post
what about Signals and Slots?
what slots and signals?
Should I connect pushbutton to qlineedit?
if your answer is yes,
Code:
QObject::connect(pushButton,
SIGNAL(clicked
()), nameEdit,
SLOT( ?????????
));
what should be the slot for nameEdit? I'm using the Designer to create the interface.
Renan
Re: QSqlQuery and QLineEdit
for what purpose do you need this connection?
Re: QSqlQuery and QLineEdit
I thought I would need it to make the result to show in the QLineEdit. -- Now I know it isn't necessary.
But my problem still persist, no result in QLineEdit.
Any ideas?
Renan
Re: QSqlQuery and QLineEdit
check the result of your query in sqlbrowser which is located in QTDIR/demos/sqlbrowser.
Re: QSqlQuery and QLineEdit
Could you please say what exactly did you do to eliminate the "Driver not loaded" message?
Re: QSqlQuery and QLineEdit
here is spirit code:
Code:
void testeMYSQL2::on_pushButton_clicked()
{
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << query.lastError().text();
return;
}
if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
qDebug() << query.lastError().text();
return;
}
if (!query.next()) {
qDebug() << query.lastError().text();
return;
}
QString name
= query.
value(0).
toString();
nameEdit->setText(name);
}
its code didn't compile, than I changed to:
Code:
void testeMYSQL2::on_pushButton_clicked()
{
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << db.lastError().text();
return;
}
if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
qDebug() << query.lastError().text();
return;
}
if (!query.next()) {
qDebug() << query.lastError().text();
return;
}
QString name
= query.
value(0).
toString();
nameEdit->setText(name);
}
and then I got this error:
Quote:
gul@gul-laptop:~/qt/testemysql2$ ./testemysql2
QSqlQuery::exec: database not open
"Driver not loaded Driver not loaded"
Then I saw where is the error:
Code:
void testeMYSQL2::on_pushButton_clicked()
{
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << db.lastError().text();
return;
}
if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
qDebug() << query.lastError().text();
return;
}
if (!query.next()) {
qDebug() << query.lastError().text();
return;
}
QString name
= query.
value(0).
toString();
nameEdit->setText(name);
Then it worked, but still I don't see the result in the QLineEdit.
Now I will try what spirit said above. I will post the result in a few seconds.
Renan