NSInteger 和 int 给出例外

发布于 2024-11-28 21:06:17 字数 995 浏览 1 评论 0原文

我正在尝试使用 FMDB 从数据库中获取数据,但尝试了一段时间却徒劳无功,我不明白为什么会抛出 EXC_BAD_ACCESS 异常。该参数是从 NSInteger 传入的,但我既无法记录它,也无法在 SELECT 语句中使用它。我的代码是:

-(SQLiteResult*) getClient:(bool)forceRefresh id:(NSString*)id forYear:(NSInteger)year {
    SQLiteResult *res;
    if(!forceRefresh){
        NSMutableArray *hereditaments = [[NSMutableArray alloc] init];
        NSLog([NSString stringWithFormat:@"SELECT * FROM Clients WHERE ID = %@ AND Year = %@", id, [NSString stringWithFormat:@"%@", year]]); // exception
        NSLog([NSString stringWithFormat:@"SELECT * FROM Clients WHERE ID = %@ AND Year = %@", id, year]]); // also exception
        FMResultSet *rs = [db executeQuery:[NSString stringWithFormat: @"SELECT * FROM Clients WHERE ID = ? AND Year = %@", year], id]; // also exception
        FMResultSet *rs = [db executeQuery:[NSString stringWithFormat: @"SELECT * FROM Clients WHERE ID = ? AND Year = ?", id, year]]; // also exception

我即将将其作为字符串传递。有没有一种安全的方法可以在 SQL 语句中使用年份?

I'm trying to get data from a database using FMDB but having tried with futility for a while, I can't figure out why this is throwing an EXC_BAD_ACCESS exception. The parameter is passed in from an NSInteger, but I can neither log it nor use it in a SELECT statement. The code I have is:

-(SQLiteResult*) getClient:(bool)forceRefresh id:(NSString*)id forYear:(NSInteger)year {
    SQLiteResult *res;
    if(!forceRefresh){
        NSMutableArray *hereditaments = [[NSMutableArray alloc] init];
        NSLog([NSString stringWithFormat:@"SELECT * FROM Clients WHERE ID = %@ AND Year = %@", id, [NSString stringWithFormat:@"%@", year]]); // exception
        NSLog([NSString stringWithFormat:@"SELECT * FROM Clients WHERE ID = %@ AND Year = %@", id, year]]); // also exception
        FMResultSet *rs = [db executeQuery:[NSString stringWithFormat: @"SELECT * FROM Clients WHERE ID = ? AND Year = %@", year], id]; // also exception
        FMResultSet *rs = [db executeQuery:[NSString stringWithFormat: @"SELECT * FROM Clients WHERE ID = ? AND Year = ?", id, year]]; // also exception

I'm on the verge of just passing it in as a string instead. Is there a safe way I can just use the year in the SQL statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱要勇敢去追 2024-12-05 21:06:17

NSInteger 不是对象类型。它只是一个 int 类型的 typedef。因此,您不能使用 %@ 作为格式说明符。您应该使用%d

NSInteger is not an object type. Its just a typedef of type int. So, you can not use %@ as the format specifier. You should use %d.

找个人就嫁了吧 2024-12-05 21:06:17

由于您的年份变量是 NSInteger 类型,因此您应该在 NSString 创建中使用 %d 格式而不是 %@

As your year variable is of NSInteger type you should use %d format and not %@ in your NSString creation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文