Results 1 to 2 of 2

Thread: SQL query - navigation buttons

  1. #1
    Join Date
    Aug 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default SQL query - navigation buttons

    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:rev()
    {
    query.previous();
    }

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

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

  2. #2
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL query - navigation buttons

    Quote Originally Posted by apffal View Post
    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.

Similar Threads

  1. MS SQL Query
    By baray98 in forum General Programming
    Replies: 0
    Last Post: 14th July 2009, 04:25
  2. query prepare with sql math
    By GuL in forum Newbie
    Replies: 19
    Last Post: 2nd September 2008, 21:41
  3. QTableWidget Sql Query 25.000 records
    By aekilic in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2008, 14:54
  4. Sql Query Problem
    By MrShahi in forum Qt Programming
    Replies: 6
    Last Post: 30th May 2008, 09:16
  5. QTreeView with results from an SQL query.
    By ederbs in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2007, 02:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.