问题描述
我正在我的 iphone 应用程序中试验 sqlite 问题.读取部分没问题,但是当我尝试使用 UPADATE 命令写入数据库时(我也尝试使用 INSERT),sql 语句返回错误 sqlite3_errmsg:
I am experimenting a problem with sqlite in my iphone app. Read part is ok, but when I try to write to a database using UPADATE command (I try with INSERT too), the sql sentence give me back the error sqlite3_errmsg:
Failed. Error is: near "UP": syntax error
代码是:
-(void)ratesInfotoDb:(NSString *)idx rate:(NSString*)rate{
query =@"UPDATE rules set rate = 5 where _id = 1994";
sqlite3_stmt *statement;
sqlite3_prepare_v2(_database, [query UTF8String], SQLITE_OPEN_READWRITE, &statement, nil);
if(sqlite3_step(statement) == SQLITE_DONE ) {
NSLog(@"element added");
}
else{
NSLog( @"Failed. Error is: %s", sqlite3_errmsg(_database));
}
sqlite3_finalize(statement);
sqlite3_close(_database);
}
查询命令UPDATE rules set rate = 5 where _id = 1994"(直接在数据库上执行)有效,所以我认为查询是正确的.
The query command "UPDATE rules set rate = 5 where _id = 1994" (executed directly on a database), works, so I suppose query is corect.
有人可以帮我吗?
推荐答案
问题很简单.您将错误的值传递给 sqlite3_prepare_v2
函数.第三个参数应该代表查询字符串的长度.
The problem is simple. You are passing the wrong value to the sqlite3_prepare_v2
function. The 3rd parameter should represent the length of the query string.
宏 SQLITE_OPEN_READWRITE
的值为 2
,因此您声明查询只有 2 个字节长,这是不正确的.
The macro SQLITE_OPEN_READWRITE
has a value of 2
so you are stating that the query is only 2 bytes long which is not true.
更改对 sqlite3_prepare_v2
的调用,如下所示:
Change your call to sqlite3_prepare_v2
as follows:
sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil);
传递-1
告诉sqlite3_prepare_v2
获取查询的实际长度(可能通过使用strlen
).
Passing -1
tells sqlite3_prepare_v2
to get the actual length of the query (probably by using strlen
).
顺便说一句 - 始终检查 sqlite3_prepare_v2
的返回值并确保它返回 SQLITE_OK
.
BTW - always check the return value of sqlite3_prepare_v2
and make sure it returns SQLITE_OK
.
这篇关于Sqlite“附近"iphone 语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!