开发者

sqlite3_bind_parameter_index returns 0?

The count returns a count of the parameters and is good. However the 开发者_如何学运维index is returning 0. Any ideas?

sqlite3 *database;
sqlite3_stmt *updateStmt;
int ID;
const char *sql;

sql = "update User set Name = ? , Dev = ?,ActiveLevel = ? Where _id = ?";


if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
  NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));

NSLog(@"count %d",sqlite3_bind_parameter_count(updateStmt));

NSLog(@"Index %d",sqlite3_bind_parameter_index(updateStmt,"ActiveLevel"));


From the fine manual:

int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
Return the index of an SQL parameter given its name.

And for parameters:

?
A question mark that is not followed by a number creates a parameter with a number one greater than the largest parameter number already assigned.
...
:AAAA
A colon followed by an identifier name holds a spot for a named parameter with the name :AAAA.

Emphasis mine in the second section.

Your SQL doesn't have any named parameters at all, you just have plain old placeholders. The parameter name is the name of the placeholder (:AAAA), not the name of the column in question; remember that you can use placeholders in places where no name could be automatically derived so you have to name them yourself.

If you want to use ActiveLevel as a named parameter, then your SQL should look like this:

update User set Name = ? , Dev = ?, ActiveLevel = :ActiveLevel Where _id = ?

And you'd probably want to replace the other placeholders (?) with named parameters for consistency.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜