dev6546
dev6546

Reputation: 1442

error: near "?": syntax error using sqlite and obj-c

After a suggestion on here I have tried to bind the values being passed into my queries, but I keep getting the syntax error: error: near "?": can someone explain why please?

NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"banklist" ofType:@"sqlite3"];
    if(sqlite3_open([sqLiteDb UTF8String], &_database) == SQLITE_OK)
    {
        {
            // prep statement
            sqlite3_stmt    *statement;
            NSString *querySQL = @"UPDATE ? SET recipe_name=? WHERE cell_id=?";
            NSLog(@"query: %@", querySQL);
            const char *query_stmt = [querySQL UTF8String];

        // preparing a query compiles the query so it can be re-used.
        sqlite3_prepare_v2(_database, query_stmt, -1, &statement, NULL);
        sqlite3_bind_text(statement, 1, [del.dayName UTF8String], -1, SQLITE_STATIC);
        sqlite3_bind_text(statement, 2, [info.name UTF8String], -1, SQLITE_STATIC);
        sqlite3_bind_int(statement, 1, del.tableRowNumber);

        // process result
        if (sqlite3_step(statement) != SQLITE_DONE)
        {
            NSLog(@"error: %s", sqlite3_errmsg(_database));
        }

        sqlite3_finalize(statement);
    }

    {
        // prep statement
        sqlite3_stmt    *statement;
        NSString *querySQL = @"UPDATE ? SET recipe_id = (SELECT key FROM recipes WHERE name = ?.recipe_name)";
        NSLog(@"query: %@", querySQL);
        const char *query_stmt = [querySQL UTF8String];

        // preparing a query compiles the query so it can be re-used.
        sqlite3_prepare_v2(_database, query_stmt, -1, &statement, NULL);
        sqlite3_bind_text(statement, 1, [del.dayName UTF8String], -1, SQLITE_STATIC);
        sqlite3_bind_text(statement, 2, [del.dayName UTF8String], -1, SQLITE_STATIC);

        // process result
        if (sqlite3_step(statement) != SQLITE_DONE)
        {
            NSLog(@"error: %s", sqlite3_errmsg(_database));
        }

        sqlite3_finalize(statement);
    }
}

sqlite3_close(_database);

Upvotes: 1

Views: 1822

Answers (2)

rmaddy
rmaddy

Reputation: 318955

You need to bind a value for each ? in the query. The 2nd parameter to the sqlite3_bind_xxx function is the index number. These are 1-based indexes.

In you 1st query you bind a value to the 1st ? two times. You probably need to change the sqlite3_bind_int call to pass 3 as the index instead of 1.

sqlite3_bind_int(statement, 3, del.tableRowNumber);

One other possible issue is your use of sqlite3_bind_text for the table name. This will put the table name in quotes. As suggested by Rob, you should use a string format to apply the table name but use sqlite3_bind_xxx for actual values you need in your query.

Upvotes: 1

Rob
Rob

Reputation: 438477

You use sqlite3_bind_xxx for values in your SQL, but not for table names. You have to use stringWithFormat for the table names and sqlite3_bind_xxx for the values.

Upvotes: 2

Related Questions