我正在尝试从单个表sqlite数据库中检索数据,但是(sqlite3_step(statement)== SQLITE_ROW)始终返回false。我已经手动检查了数据库中的值,并且它们存在。这是完整的代码:
-(NSArray *)findByPartNumber:(NSString *)partNumber
{
const char *dbpath = [databasePath UTF8String];
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
if(sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"SELECT Description, ListPrice FROM PriceList WHERE PartNumber=?"];
const char *query_stmt = [querySQL UTF8String];
if(sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
sqlite3_bind_text(statement, 1, [partNumber UTF8String], -1, SQLITE_STATIC);
if(sqlite3_step(statement) == SQLITE_ROW)
{
NSString *description;
NSString *listPrice;
const char *tmp1 = (const char *)sqlite3_column_text(statement, 0);
const char *tmp2 = (const char *)sqlite3_column_text(statement, 1);
if(tmp1 == NULL)
description = nil;
else
{
description = [[NSString alloc] initWithUTF8String:tmp1];
[resultArray addObject:description];
}
if(tmp2 == NULL)
listPrice = nil;
else
{
listPrice = [[NSString alloc] initWithUTF8String:tmp2];
[resultArray addObject:listPrice];
}
sqlite3_finalize(statement);
}
}
sqlite3_close(database);
}
if([resultArray count] == 0)
return nil;
else
return resultArray;
}
该代码进入sqlite3_prepare_v2块,但跳过sqlite3_step块。我想念什么吗?
最佳答案
您的实现存在问题。
你写得像:
sqlite3_bind_text(statement, 1, [partNumber UTF8String], -1, SQLITE_STATIC);
if(sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
...
}
您只能将值绑定到准备好的语句,因此将上面的代码更改为:
if(sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
sqlite3_bind_text(statement, 1, [partNumber UTF8String], -1, SQLITE_STATIC);
...
}
关于ios - sqlite3_step始终返回SQLITE_DONE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26859366/