【问题标题】:SQLITE (fmdb) select where x is null not workingSQLITE(fmdb)选择x为空的地方不起作用
【发布时间】:2012-10-30 21:05:16
【问题描述】:

我在 iOS 上使用 FMDB,尝试使用如下语句查询表,

select * from test where foo isNull

在 sqlite C API 中,这最终会使用此 API 绑定到 null,

int sqlite3_bind_null(sqlite3_stmt*, int);

这行不通。通过 fmdb 调用的 select 似乎将列正确绑定到 null 没有找到匹配的记录。

如果在 sqlite3 命令行会话中执行以下命令,它可以工作,但不能通过 FMDB 的 sqlite C API。

select * from test where foo isNull;

这里是重现问题的 fmdb 代码。看起来 fmdb 正在执行调用 sqlite3_bind_null 的正确步骤。

        NSString *stmt = @"select * from test where shipToCode=:foo";
        NSDictionary *dict = @{@"foo" : [NSNull null]};
        FMResultSet *rs = [db executeQuery:stmt withParameterDictionary:dict];
        int count = 0;
        while ([rs next]) {
            count++;
        }
        NSLog(@"Count %d", count);

【问题讨论】:

    标签: ios sqlite fmdb isnull


    【解决方案1】:

    NULL 不能与= 运算符进行比较;它不等于 any 值,甚至它本身。

    要与NULL 比较,您必须使用IS NULL 运算符:

    stmt = @"select * from test where shipToCode is null";
    

    【讨论】:

    • 我自己发现了这个。描述得很好。
    【解决方案2】:

    尝试以下方法并检查是否有任何变化:

    NSString *query = @"SELECT * FROM test WHERE shipToCode is NULL";
    
    FMResultSet *rs = [db executeQuery:query];
    int count = 0;
    
    while ([rs next]) {
        count++;
    }
    
    NSLog(@"Count %d", count);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      相关资源
      最近更新 更多