Ios 如何发现SQLITE查询结果为空?
我在我的应用程序中使用了以下方法Ios 如何发现SQLITE查询结果为空?,ios,objective-c,sqlite,Ios,Objective C,Sqlite,我在我的应用程序中使用了以下方法 -(int) getMax:(NSString *)subItemName second :(int) tableID { int count1=0; sqlite3_stmt *statement; NSString *query = [NSString stringWithFormat:@"Select MAX(NoOfItems) from billTable where SubItemName='%@' AND TableID='
-(int) getMax:(NSString *)subItemName second :(int) tableID
{
int count1=0;
sqlite3_stmt *statement;
NSString *query = [NSString stringWithFormat:@"Select MAX(NoOfItems) from billTable where SubItemName='%@' AND TableID='%d'",subItemName,tableID];
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)== SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
count1 = [[NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 0)]intValue];
}
sqlite3_step (statement);
}
return count1;
}
正如您所看到的,my查询将返回行数(一个整数),假设my查询返回一个NULL
值(如果我的数据库中where子句条件不存在这样的条件,它将返回NULL
值。如何在执行查询后检查它返回NULL
?您可以调用以了解列值的类型:
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)== SQLITE_OK) {
if (sqlite3_step(statement) == SQLITE_ROW) {
if (sqlite3_column_type(statement, 0) == SQLITE_NULL)
count1 = -1; // or whatever
else
count1 = sqlite3_column_int(statement, 0);
}
sqlite3_finalize(statement);
}
为什么要在两个位置调用
sqlite3\u step
?第二个应该是sqlite3\u finalize
。不要使用stringWithFormat:
构建查询。正确使用sqlite3\u bind\u xxx
函数将值绑定到查询。