PDA

View Full Version : QSqlQuery and QLineEdit



GuL
14th August 2008, 17:14
Why this code doesn't work?
I want to show the result of this query in the QLineEdit.


void testeMYSQL2::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("");
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"));
}
*/
QSqlQuery query;
query.exec("select nome_prod from produtos where cod_prod = 1");
query.next();
for (int i = 0; i < record.count(); i++) {
QString name = query.value(i).toString();
nameEdit->setText(name);
}

What I am doing wrong?

Renan

spirit
14th August 2008, 17:29
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


....
while (query.next()) {
QString data = query.value(0).toString();//first column == nome_prod
....
}
....


read for detailes QSqlQuery::next

GuL
14th August 2008, 17:34
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

spirit
14th August 2008, 17:39
if you need only one result, then


query.next();
QString res = query.valut(0).toString();

wysota
14th August 2008, 17:39
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.

GuL
14th August 2008, 17:52
New code and still no result:


void testeMYSQL2::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
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"));
}
*/
QSqlQuery query;
query.exec("select nome_prod from produtos where cod_prod = 1");

query.next();
QString name = query.value(0).toString();
nameEdit->setText(name);
}

spirit
14th August 2008, 17:57
void testeMYSQL2::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << query.lastError().text();
return;
}

QSqlQuery query;
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.

GuL
14th August 2008, 18:18
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

spirit
14th August 2008, 18:27
did you have compiled plugin or qt with mysql driver?

GuL
14th August 2008, 18:40
It is working.


#
void testeMYSQL2::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << query.lastError().text();
return;
}

QSqlQuery query;
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.

GuL
14th August 2008, 19:05
what can I do to get those lastError() ???

Renan

spirit
14th August 2008, 19:12
does qDebug() << db.lastError().text(); return nothing?
if QSqlDatabase::lastError doesn't return anything then everything is ok.

GuL
14th August 2008, 19:24
ok, but

if everything is ok, why QLineEdit doesn't show the result?

what about Signals and Slots?


Renan

spirit
14th August 2008, 19:28
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?




what about Signals and Slots?


what slots and signals?

GuL
14th August 2008, 19:36
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:
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,

QObject::connect(pushButton, SIGNAL(clicked()), nameEdit, SLOT( ????????? ));

what should be the slot for nameEdit? I'm using the Designer to create the interface.

Renan

spirit
14th August 2008, 19:39
for what purpose do you need this connection?

GuL
14th August 2008, 19:51
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

spirit
14th August 2008, 19:53
check the result of your query in sqlbrowser which is located in QTDIR/demos/sqlbrowser.

wysota
14th August 2008, 19:58
Could you please say what exactly did you do to eliminate the "Driver not loaded" message?

GuL
14th August 2008, 20:10
here is spirit code:

void testeMYSQL2::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << query.lastError().text();
return;
}

QSqlQuery query;
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:

void testeMYSQL2::on_pushButton_clicked()
{
QSqlQuery query;
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
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:

gul@gul-laptop:~/qt/testemysql2$ ./testemysql2
QSqlQuery::exec: database not open
"Driver not loaded Driver not loaded"

Then I saw where is the error:


void testeMYSQL2::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << db.lastError().text();
return;
}

QSqlQuery query;
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

GuL
14th August 2008, 20:20
The result:

DISCO1

Any ideas?

Renan

spirit
14th August 2008, 20:31
ok. so, when you call your slot console hasn't any messages, right?

spirit
14th August 2008, 20:56
here is small example


#include <QApplication>
#include <QtGui>
#include <QtSql>

int main(int argc, char ** argv)
{
QApplication app(argc, argv);

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("test.db");

if (!db.open()) {
qDebug() << db.lastError().text();
return 0;
}

QSqlQuery query;
qDebug() << query.exec("CREATE TABLE test (id int PRIMARY KEY, test nvarchar)");

qDebug() << query.exec("INSERT INTO test VALUES(0, 'a')");
qDebug() << query.exec("INSERT INTO test VALUES(1, 'b')");
qDebug() << query.exec("INSERT INTO test VALUES(2, 'c')");
qDebug() << query.exec("INSERT INTO test VALUES(3, 'd')");
qDebug() << query.exec("SELECT test FROM test WHERE id = 1");
if (!query.next()) {
qDebug() << query.lastError().text();
return 0;
}
QLineEdit le(query.value(0).toString());
le.show();

return app.exec();
}

wysota
14th August 2008, 22:52
its code didn't compile, than I changed to:
If the code didn't compile, how come you managed to run an non-existing program? "Driver not loaded" is a runtime error, not compiler message.

GuL
15th August 2008, 13:24
Copying spirit code to my project didn't compile. here is spirit original code:


void testeMYSQL2::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << query.lastError().text();
return;
}
QSqlQuery query;
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);
}


In line 9, when we test if the database was successfully open,

query.lastError().text();
I got this error:

.\testemysql2.cpp(50) : error C2065: 'query' : undeclared identifier
.\testemysql2.cpp(50) : error C2228: left of '.lastError' must have class/struct/union
type is ''unknown-type''

when I canged line 9 to:

db.lastError().text();
I managed to run the program.

About the run-time error: "Driver not loaded"
It happened when I cut line 12 and paste it between line 2 and 3 and line 9 still was query.lastError().text();

I will try your code from the last post, spirit.
I had just install sqlite.

Renan



Renan

GuL
15th August 2008, 13:40
it shows me the letter "b" in a QLineEdit. Nice.

I will try to modify my code to work with mysql using your example.

Renan