Strange SQLite

Discussion in 'iOS Programming' started by Pinus, Dec 15, 2008.

  1. macrumors newbie

    Joined:
    Nov 25, 2008
    #1
    I have some code that works judt fine when I do a simple 'select' statement :

    const char *sql = "SELECT * FROM aliments WHERE nom_alm = ?;
    sqlite3_stmt *statement;

    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
    sqlite3_bind_text(statement, 1, [nameToSearch UTF8String], -1, SQLITE_TRANSIENT);
    ....

    The same code with the sql query : "SELECT * FROM aliments WHERE nom_alm LIKE '%?%'" returns nothing instead of 3 rows, as verified with a GUI client.

    Is there something I should now regarding "sqlite3_bind_text" when we do a "SELECT LIKE" statement ?

    Thanks.

    Pinus.
     
  2. macrumors member

    Joined:
    Aug 10, 2008
    #2
    I've had the same problem, and I don't know exactly why, but when I use a different parameter substitution approach it works properly. Try this, instead of the '?' parameter:

    Code:
    const char *sql = "SELECT * FROM aliments WHERE nom_alm = :ltr";
    
     
  3. thread starter macrumors newbie

    Joined:
    Nov 25, 2008
    #3
    Hi,

    Thanks for the tip. But it doesn't solve the issue with 'LIKE' statement.

    This one works just fine :
    Code:
    const char *sql = "SELECT * FROM aliments WHERE nom_alm = :ltr";
    But this one still fails to return any row :
    Code:
    const char *sql = "SELECT * FROM aliments WHERE nom_alm LIKE '%:ltr%'";
     
  4. thread starter macrumors newbie

    Joined:
    Nov 25, 2008
    #4
    [solved]

    I finally got it solved by appending "%" before and after "nameToSearch" before passing it to "sqlite3_bind_text".

    Pinus.
     

Share This Page