我正在尝试查询在特定列中具有字符串的行。但是,我无法获得理想的结果。
码:
[_database open];
FMResultSet *results = [_database executeQuery:@"select * from testNotes where notesid=%d",notesID];
NSString *notes=[results stringForColumn:@"notes"];
if ([_database hadError]) {
NSLog(@"DB Error %d: %@", [_database lastErrorCode], [_database lastErrorMessage]);
}
[_database close];
我使用sqlite浏览器检查了数据。我发送的值是13,表中存在数据。
当我使用时:
notesid=%d
控制台说:(这里FMResultsSet
是nil
,notes
是nil
)DB错误1:“%”附近:语法错误
我还尝试通过在=和%d之间留一个空格
当我使用:
notesid='%d'
控制台时说:(这里FMResultsSet
不是nil
,notes
是nil
)DB错误25:绑定或列索引超出范围
当我使用:
notesid='?'
控制台说DB错误25:绑定或列索引超出范围
当我使用:
notesid=?
应用程序在obj = va_arg(args, id);
文件中的FMDatabse.m
崩溃时我也尝试过这个:
[_database executeQuery:[NSString stringWithFormat:@"select * from testNotes where notesid='%d'",notesID]];
基于整数值查询行的正确方法是什么?
最佳答案
executeQuery
期望所有参数都是Objective-C对象。但是,notesID
是整数,因此不是对象。为了解决这个问题,您需要将notesID
包装在NSNumber
对象中,如下所示:
FMResultSet *results = [_database executeQuery:@"select * from testNotes where notesid=?", @(notesID)];
关于ios - 如何使用FMDB在iOS中使用整数参数查询SQlite?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34930814/