Results 1 to 20 of 20

Thread: select query with like

  1. #1
    Join Date
    Mar 2008
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Unhappy select query with like



    i have problum in finding data from mysql database & show data in listwidget with query given below

    QSqlQuery query2("SELECT title * from mediadetails where title LIKE '%:title%' ");


    i want to find data that containing specific character that mainsioned in LIKE within query


    problum comes when i add items within listWidget



    code is
    void VistaMedia::content()
    {

    QString LEdit1=searchLineEdit->text();

    QSqlQuery query2("SELECT title * from mediadetails where title LIKE '%:title%' ");
    query2.bindValue(":title",LEdit1);
    query2.exec();
    while(query2.next());
    {

    listWidget->addItem(query2.value(0).toString());
    QMessageBox::information(this,"Error","File not found");

    }
    }


    thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: select query with like

    It should be:
    Qt Code:
    1. QSqlQuery query2("SELECT title * from mediadetails where title LIKE '%' || :title || '%' ");
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. query2.bindValue(":title" "%" + LEdit1 + "%");
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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

    Default Re: select query with like

    its not working ........
    plzzzzzzzzzzz check again for any problem..if there??
    @shish

  4. #4
    Join Date
    Mar 2008
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: select query with like

    thanks for reply

    check this again

    this not working properly

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: select query with like

    Quote Originally Posted by ag.sitesh View Post
    this not working properly
    Indeed, there's another mistake. It should be:
    Qt Code:
    1. QSqlQuery query2;
    2. query2.prepare(...);
    3. query2.bindValue( ... );
    4. ...
    To copy to clipboard, switch view to plain text mode 
    QSqlQuery::QSqlQuery( const QString & ) constructor executes the query, so you can't use it with placeholders.

  6. #6
    Join Date
    Oct 2007
    Location
    Cracow
    Posts
    56
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: select query with like

    I think that problem is in SELECT
    Quote Originally Posted by jacek View Post
    Qt Code:
    1. QSqlQuery query2("SELECT title * from mediadetails where title LIKE '%' || :title || '%' ");
    To copy to clipboard, switch view to plain text mode 
    it should be comma before '*'

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: select query with like

    Quote Originally Posted by mazurekwrc View Post
    it should be comma before '*'
    Yes, that's the third problem.

  8. #8
    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 with like

    Qt Code:
    1. query2.bindValue(":title" "%" + LEdit1 + "%");
    To copy to clipboard, switch view to plain text mode 
    ... and there a comma is missing ;-)
    Qt Code:
    1. query2.bindValue(":title", "%" + LEdit1 + "%");
    To copy to clipboard, switch view to plain text mode 

    And by the way, I guess that the || is only understood by oracle. If using MySql use the CONCAT function.

    Lykurg

  9. #9
    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

    Thumbs down Re: select query with like

    its not working guys........
    its shows error unable to fetch data...........
    can u write and test any small code on any of ur table in mysql and then post it here plzzz.....
    becoz its a basic search only ...!!!
    which must be done...
    @shish

  10. #10
    Join Date
    Oct 2007
    Location
    Cracow
    Posts
    56
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: select query with like

    you put ';' after while

    Quote Originally Posted by ag.sitesh View Post
    while(query2.next());
    {
    listWidget->addItem(query2.value(0).toString());
    QMessageBox::information(this,"Error","File not found");

    }
    mazurek

  11. #11
    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

    Red face Re: select query with like

    tahts ok but....better u plzz give correct tested code block....will u???
    @shish

  12. #12
    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 Re: select query with like

    thats a mistake ok....but better u plzz give a tested code by urself using mysql.. if possible !!
    well ...i m also hanging in this query garden ...
    i havn't got my answer...
    god!!!
    @shish

  13. #13
    Join Date
    Oct 2007
    Location
    Cracow
    Posts
    56
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: select query with like

    this code working hith PostgreSql
    Qt Code:
    1. QString first_name = "arc";
    2. query.prepare( "SELECT name FROM people WHERE name LIKE :name" );
    3. query.bindValue( ":name", "%" + first_name + "%" );
    4. query.exec();
    5.  
    6. while( query.next() )
    7. {
    8. ....
    9. }
    To copy to clipboard, switch view to plain text mode 
    i hope that will help you

  14. The following user says thank you to mazurekwrc for this useful post:

    ag.sitesh (11th April 2008)

  15. #14
    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

    Smile Re: select query with like

    Thanks a lot!!!!!!!!!!!!!!!
    thats the way!!!!!!!
    now everything fine....
    @shish

  16. #15
    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

    Default Re: select query with like on Multiple conditions

    On a single text search its fine but if i have 3 lineEdits(as search criteria) for 3 columns and user can enter search text in any or every or some of those lineEdits then how to implement it???

    I have written this code : but it needs that all the lineEdits must be filled ....leaving blank any lineEdit ,then query fetches every row of the table and i want to search for any text enetered in any lineEdits ....

    void MultipleSearch::search()
    {


    }
    @shish

  17. #16
    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 Re: select query with like on Multiple conditions

    On a single text search its fine but if i have 3 lineEdits(as search criteria) for 3 columns and user can enter search text in any or every or some of those lineEdits then how to implement it???

    I have written this code : but it needs that all the lineEdits must be filled ....leaving blank any lineEdit ,then query fetches every row of the table and i want to search for any text enetered in any lineEdits ....

    void MultipleSearch::search()
    {
    QString titletxt=titlesrchTxt->text();
    QString composerTxt=composersrchTxt->text();
    QString releaseDateTxt=rdatesrchTxt->text();
    QSqlQuery query;
    QString querytxt="SELECT title FROM mediadetails WHERE title LIKE :title OR composer LIKE :composer OR releasedate LIKE :rdate";
    query.prepare(querytxt);
    query.bindValue( ":title", "%" + titletxt + "%" );
    query.bindValue( ":rdate", "%" + releaseDateTxt + "%" );
    query.bindValue( ":composer", "%" + composerTxt+ "%" );
    query.exec();
    while(query.next())
    {
    listView->addItem(query.value(0).toString());
    }
    }
    @shish

  18. #17
    Join Date
    Oct 2007
    Location
    Cracow
    Posts
    56
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: select query with like on Multiple conditions

    First of all change this line
    Quote Originally Posted by sinha.ashish View Post
    QString querytxt="SELECT title FROM mediadetails WHERE title LIKE :title OR composer LIKE :composer OR releasedate LIKE :rdate";
    to

    Qt Code:
    1. QString querytxt="SELECT title, composer, releasedate FROM mediadetails WHERE title LIKE :title OR composer LIKE :composer OR releasedate LIKE :rdate";
    To copy to clipboard, switch view to plain text mode 

    and try

  19. #18
    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

    Default Re: select query with like

    sorry ...tahts not the syntex which i was trying to post ...u said right...i just missed to ommit ';' ...but do u have idea to do multiple search????
    ..
    @shish

  20. #19
    Join Date
    Oct 2007
    Location
    Cracow
    Posts
    56
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: select query with like

    you missed 'composer' and 'releasedate' not ';'
    here is my code which work with multiple search
    Qt Code:
    1. QString first_name = "arc";
    2. QString surname = "LA";
    3. query.prepare( "SELECT name, surname FROM people WHERE ( ( name LIKE :name ) OR ( surname LIKE :surname ) )" );
    4. query.bindValue( ":name", "%" + first_name + "%" );
    5. query.bindValue( ":surname", "%" + surname + "%" );
    6. query.exec();
    To copy to clipboard, switch view to plain text mode 

  21. The following user says thank you to mazurekwrc for this useful post:

    sinha.ashish (14th April 2008)

  22. #20
    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

    Smile Re: select query with like

    thanks....its working fine...
    @shish

Similar Threads

  1. Unable to fetch data when searching through select query
    By sinha.ashish in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2008, 14:06
  2. Select query using mysql for searching
    By sinha.ashish in forum Qt Programming
    Replies: 4
    Last Post: 10th April 2008, 06:14
  3. Sql Server cannot retrieve data from select
    By Atomino in forum Qt Programming
    Replies: 10
    Last Post: 7th September 2006, 16:37
  4. Aborting a query set on a TableModel
    By Quid in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2006, 22:36
  5. Record update windowd entered data saving
    By MarkoSan in forum Qt Programming
    Replies: 56
    Last Post: 18th January 2006, 18:50

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.