Results 1 to 5 of 5

Thread: Select query using mysql for searching

  1. #1
    Join Date
    Jan 2008
    Location
    India
    Posts
    31
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Select query using mysql for searching

    Qt Code:
    1. void VistaMedia::search()
    2. {
    3. QString searchText=searchTextLineEdit->text();
    4. QSqlQuery query;
    5. query.prepare("SELECT * FROM mediadetails WHERE title LIKE %:title% ");
    6. query.bindValue(":title",searchText);
    7. query.exec();
    8. while(query.next())
    9. listWidget->addItem(query.value(1).toString());
    10. QSqlError e=query.lastError () ;
    11. QMessageBox::information(this,"problem",e.text());
    12. }
    To copy to clipboard, switch view to plain text mode 
    above function shows error message box as "syntax error near %" after calling. Help me to write the correct syntax or anyclues for searching using wildcards!!!!!!!!
    Last edited by marcel; 9th April 2008 at 07:59. Reason: missing [code] tags
    @shish

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Select query using mysql for searching

    The query should be:
    SELECT * FROM mediadetails WHERE title LIKE '%:title%'

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Select query using mysql for searching

    ...and better don't use the asterisk in conjunction with query.value(1). If You later change your table layout, the code will behave incorrect. Use "SELECT `myColumnNameWhichIWant`..." and query.value(0) in your case.

    Lykurg

  4. #4
    Join Date
    Jan 2008
    Location
    India
    Posts
    31
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Unhappy Re: Select query using mysql for searching

    void VistaMedia::search()
    {
    QString searchText=searchTextLineEdit->text();
    QSqlQuery query;
    query.prepare("SELECT title FROM mediadetails WHERE title LIKE ' %:title% '"); query.bindValue(":title",searchText);
    query.exec();
    while(query.next())
    listWidget->addItem(query.value(0).toString());
    QSqlError e=query.lastError () ;
    QMessageBox::information(this,"problem",e.text());
    }



    table details are as :
    mediadetails
    ----------------
    |title|

    F_1.avi
    F_2.avi
    11.dat
    music.dat
    jazz.avi

    i am entering in the search text " f ".

    thanks for the suggesstions ....but even after doing so....there is a new error message shows "unable to fetch data". what to do????
    Last edited by sinha.ashish; 10th April 2008 at 05:37.
    @shish

  5. #5
    Join Date
    Jan 2008
    Location
    India
    Posts
    31
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Talking Re: Select query using mysql for searching

    void VistaMedia::search()
    {
    QString serachText=searchTextLineEdit->text().trimmed();
    int noOfItems=listWidget->count();
    while(noOfItems>0)
    {
    if(!(listWidget->item(noOfItems-1)->text().contains(mname, Qt::CaseInsensitive)))
    {
    listWidget->removeItemWidget ( listWidget->item(noOfItems-1));
    delete listWidget->item(noOfItems-1);
    }
    l--;
    }
    }


    I have solved my problem in another way ,just using listwidget's functions,without using query.its fast and better.Thanks to all replies ....
    @shish

Similar Threads

  1. INSERT query with MySQL problem
    By timmyg in forum Qt Programming
    Replies: 10
    Last Post: 20th March 2008, 21:52
  2. Mysql query question
    By twells55555 in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2007, 23: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.