Results 1 to 3 of 3

Thread: SQL Select statement with binary comparison

  1. #1
    Join Date
    Jan 2009
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default SQL Select statement with binary comparison

    I have an SQL table (PEOPLE) with a binary column (BINID, BINARY(4)). I want to run a SELECT statement with filter on that column, but no luck yet. And I'm getting pretty desperate.

    When I do SELECT without WHERE, and do 'if' comparison afterwards I get the required result:
    Qt Code:
    1. queryStr = QString("SELECT NAME, BINID FROM PEOPLE");
    2. query.exec(queryStr);
    3. qDebug() << queryStr << " result size: " << query.size();
    4. while (query.next()) {
    5. QString name = query.record().value(0).toString();
    6. QByteArray binid = query.record().value(1).toByteArray();
    7. if (binid == myBinID)
    8. qDebug() << " - match: " << name;
    9. }
    To copy to clipboard, switch view to plain text mode 
    with result:
    Qt Code:
    1. "SELECT NAME, BINID FROM PEOPLE" result size: 5
    2. - match: "Jo"
    To copy to clipboard, switch view to plain text mode 

    However, with WHERE statement I get no results:
    Qt Code:
    1. QByteArray queryBA("SELECT NAME FROM PEOPLE WHERE BINID='");
    2. queryBA.append(myBinID);
    3. queryBA.append("'");
    4. query.exec(queryBA);
    5. qDebug() << queryBA << " result size: " << query.size();
    To copy to clipboard, switch view to plain text mode 
    with:
    Qt Code:
    1. "SELECT NAME FROM PEOPLE WHERE BINID='ÿÃ'" result size: 0
    To copy to clipboard, switch view to plain text mode 

    Any help would be appreciated. Attached is the source file + MySQL database if you wish to try.

    Thanks guys.
    Attached Files Attached Files

  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: SQL Select statement with binary comparison

    That happens because the bytearray is converted to a unicode character string. Why dont't you use parameterized queries (with bindValue)?

  3. The following user says thank you to marcel for this useful post:

    xfurrier (21st June 2009)

  4. #3
    Join Date
    Jan 2009
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: SQL Select statement with binary comparison

    Hi Marcel,

    Thanks for that. I've tried bindValue before, but did a silly mistake. Rather than using :binid I had ':binid' in SELECT statement.

Similar Threads

  1. Sql Server cannot retrieve data from select
    By Atomino in forum Qt Programming
    Replies: 10
    Last Post: 7th September 2006, 16:37

Tags for this Thread

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.