Results 1 to 6 of 6

Thread: QSqlQuery problem

  1. #1
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QSqlQuery problem

    Hello anyone,

    Iám newbe,

    I have a mainWindow with a lineEdit called leFirst.
    Ihave also a dialog called searchDialog with one combobox with 4 different names.(items)
    When i call the dialog in my MainWindow en select one item on the combobox i want that item been search in my database and put the result to my lineEdit in my MainWindow.
    code snippet:
    Qt Code:
    1. void MainWindow::search()
    2. {
    3. searchDialog dlg(this);
    4. if( dlg.exec() == QDialog::Accepted) {
    5.  
    6. QSqlQuery query("SELECT firstname FROM person");
    7. QString name = dlg.nameComboBox->currentText();
    8. if ( query.next() )
    9. name = (query.value(0).toString());
    10. leFirst->setText( name);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    This works fine only for the first name in the combobox and showing up in the lineEdit.
    When i choose for the second name in the combobox ( the second item) then in lineEdit only shows the first name again and not the second name.
    Is something wrong in my code or missing something.
    I have tried severall options but nothing works.

    Thanks in advance.
    Last edited by wysota; 21st December 2006 at 21:46. Reason: reformatted to look better

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery problem

    Your code does not more, not less than:
    1. Get "firstname" field from all records from the database
    2. Store the text from the combobox in variable "name"
    3. If there are any rows in the database...
    a) assign the value of the first field in the first row of the query result to the variable "name"
    b) set the contents of the line edit to the contents of the variable "name"

    Bottom line:
    You always set the value from the first row of the database regardless which item is selected in the combobox. You're not searching anything...

  3. #3
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlQuery problem

    I don't understand what you mean with assign en contents.
    Can you give me a little example?.
    Thank in advance.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery problem

    Quote Originally Posted by dragon View Post
    I don't understand what you mean with assign en contents.
    Can you give me a little example?
    Here you assign to the variable called name:
    Qt Code:
    1. name = (query.value(0).toString());
    To copy to clipboard, switch view to plain text mode 

    If you want to make a search you need something like this:
    Qt Code:
    1. while ( query.next() )
    2. if(name == query.value(0).toString()){
    3. leFirst->setText( name );
    4. break;
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    Do you see the difference?

  5. #5
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlQuery problem

    Yes i see the differents.
    It works fine.
    Thak you for your time and answer.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery problem

    Of course it would be simpler to do it this way:

    Qt Code:
    1. void MainWindow::search() {
    2. searchDialog dlg(this);
    3. if( dlg.exec() == QDialog::Accepted) {
    4. QSqlQuery query;
    5. query.prepare("SELECT firstname FROM person WHERE firstname==:name");
    6. query.bindValue(":name", dlg.nameComboBox->currentText());
    7. if(query.exec() && query.hasNext())
    8. leFirst->setText( name);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.