简体   繁体   English

SQLite结果集包含空值

[英]Sqlite result set contain a null value

I get a result set from sqlite which looks something like this: 我从sqlite得到一个结果集,看起来像这样:

Null, 23, 34, 45 (being (say) 4 items returned) Null,23、34、45(正在(说)返回4个项目)

The null value is correct - however when I try to add it to an array in objective-c with the following code I get an error - Null cstring - here's my code: 空值是正确的-但是,当我尝试使用以下代码将其添加到Objective-C中的数组时,出现错误-空cstring-这是我的代码:

[array addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]]; [array addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)]]];

I tried unsuccessfully testing for the null before adding it to the array and replacing it with a zero but so far no luck. 我尝试将null添加到数组中并将其替换为零,但到目前为止没有运气,但未成功测试null。

Could someone please help me to handle the null value so I can add maybe a zero into the array. 有人可以帮我处理空值,以便我可以在数组中添加零。

Thanks for any help. 谢谢你的帮助。

As Hot Licks said, check the return value and either add a string created from that, or add [NSNull null] . 正如Hot Licks所说,检查返回值并添加从中创建的字符串,或者添加[NSNull null]

const unsigned char *column0 = sqlite3_column_text(statement, 0);
if (column0)
    [array addObject:[NSString stringWithUTF8String:(const char *)column0]];
else
    [array addObject:[NSNull null]];

Handling null as suggested is a good option. 按照建议处理null是一个不错的选择。

You can also modify queries to force column to always be TEXT using CAST(column_name AS TEXT) , or force NULLs to empty strings using COALESCE(column_name, '') . 您还可以修改查询以使用CAST(column_name AS TEXT)强制将列始终为TEXT ,或使用COALESCE(column_name, '')强制将NULL强制为空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM