PDA

View Full Version : SQL query - navigation buttons



apffal
4th August 2009, 17:10
I can connect to a mysql database and make queries successfully - but, with my search function (associated to a search button) only get the first record in each query result.
So, I added to my form 4 another functions / buttons, for navigating to other (first, previous, next and last) query result records.
However, when compiling, I get, for those 4 other functions, a "query not declared in this scope" error.
How to solve this ?
Thanks in advance

That's my actual code



#include <QtGui>
#include <QtSql>
#include "legis.h"

legis::legis()
{
connect( Search, SIGNAL( clicked() ), this, SLOT( search() ) );
connect( First, SIGNAL( clicked() ), this, SLOT( first() ) );
connect( Prev, SIGNAL( clicked() ), this, SLOT( prev() ) );
connect( Next, SIGNAL( clicked() ), this, SLOT( next() ) );
connect( Last, SIGNAL( clicked() ), this, SLOT( last() ) );
}

void legis::search()
{

QSqlDatabase db;
QString str, str_1, str_2, str_3, field, value;
int num;

db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("jur");
db.setUserName("antonio");
db.setPassword("");
db.open();

if(db.isOpen()){
QMessageBox::information( this, "result", "connected" );
}
else {
QMessageBox::information( this, "result", "failed" );
}

field = fields->currentText();
value = exp->text();
QSqlQuery query("SELECT *FROM legis WHERE " + field + " LIKE '%" + value + "%'", db);
query.next();
str = query.value(0).toString();
ord-> setText(str);
str_1 = query.value(1).toString();
dip-> setText(str_1);
str_2 = query.value(2).toString();
sum-> setText(str_2);
str_3 = query.value(3).toString();
not-> setText(str_3);
num = query.size( );
reg-> setText(QString("%L1").arg(num));
}
void legis::first()
{
query.first();
}

void legis::prev()
{
query.previous();
}

void legis::next()
{
query.next();
}

void legis::last()
{
query.last();
}

jpujolf
5th August 2009, 09:38
However, when compiling, I get, for those 4 other functions, a "query not declared in this scope" error.
How to solve this ?

As shown in your code "query" is declared as a local variable inside search method ( in the stack ), so in all the other methods you are trying to use an undeclared variable.

You MUST declare query as a member of the class, like a global variable, but inside the class itself.

Try declaring it i.e. on the private or protected section of class declaration, at it must work fine.