Martin
Martin

Reputation: 117

sqlite with LIKE statement in xcode for iphone

First (of many?) posts here.

I have an issue with my statement.

sqlite3_stmt *statement;
    const char *query = "select * from words where ordet LIKE ?001";

    NSString *wildcard = [NSString stringWithFormat:@"%@%%", fullWordForSQL.text];
    sqlite3_bind_text(statement, 1, [wildcard UTF8String], -1, SQLITE_STATIC);

    if (sqlite3_prepare_v2(dbHandle, query, -1, &statement, NULL) == SQLITE_OK ){
        NSLog(@"in if");
        NSLog(@"Data: %s", query);

I have successfully retrieved data with the query:

const char *query = "select * from words where ordet LIKE "_test__";

But when I try to bind the dynamicly created word it doesnt stick. My nslog prints "Data: select * from words where ordet LIKE ?001" so somehow I am not getting the bind to work. I know that there will be other issues along the way with the code above but right now Im stuck at this problem

Upvotes: 1

Views: 2121

Answers (4)

Seoras
Seoras

Reputation: 1277

I think this is much simpler.

NSString *query = [NSString stringWithFormat:@"SELECT * FROM words WHERE ordet GLOB '*%@*' ", fullWordForSQL.text];

Upvotes: 0

ragamufin
ragamufin

Reputation: 4162

Try doing your prepare_v2 statement BEFORE your bind_text statment.

Upvotes: 0

Tim
Tim

Reputation: 8921

Wouldn't you need to surround the value in single-quotes? " where ordet like '%001' " Or is that unnecessary in xcode? I am not proficient at xcode. Are you trying to inject the wildcard character into the sql statement in front of a value that is already contained in the statement string, or are you trying to prepend the wildcard character to a search-value and then bind that search-value-with-wildcard to the statement? It looks like the former to me, but as I said, I don't know xcode. I would not normally include a piece of the search-term in the query statement, but would do this:

       "select * from words where ordet like ?"

      // prepend the wildcard to my search term
      // bind the search term to my query

Upvotes: 1

Rob Napier
Rob Napier

Reputation: 299345

query does not change when you call sqlite3_bind_text(). If you think about it, there's no way it should change, since you declared it as a const char *. What you want to do is use sqlite3_trace() to register to get a callback with the final bound string so you can see what's really happening.

Upvotes: 2

Related Questions