FMDB 插入數(shù)據(jù)報(bào)錯(cuò)

1、錯(cuò)誤場景

FMDB 框架本身是一個(gè)簡單易用的框架。

最近,在執(zhí)行insert Sql 語句的時(shí)候經(jīng)常報(bào)錯(cuò)。

特別是NSInterger的數(shù)據(jù),在插入的時(shí)候容易入庫失敗。

2、錯(cuò)誤分析

官方文檔,對(duì)于基本數(shù)據(jù)的插入,單獨(dú)寫了一個(gè)示例部分。

明確指出,在執(zhí)行insert 的SQL語句的時(shí)候,

NSInterger 需要是NSNumber對(duì)象插入。

3、解決方法——官方文檔

官方示例代碼:

NSInteger identifier = 42;

NSString *name = @"Liam O'Flaherty ("the famous Irish author")";

NSDate *date = [NSDate date];

NSString *comment = nil;

BOOL success = [db executeUpdate:@"INSERT INTO authors
(identifier, name, date, comment) VALUES (?, ?, ?, ?)",
@(identifier), name, date, comment ?: [NSNull null]];

if (!success) {
NSLog(@"error = %@", [db lastErrorMessage]);
}

官方解釋:

Note: Fundamental data types, like the NSInteger variable 
identifier, should be as a NSNumber objects, achieved by 
using the @ syntax, shown above. Or you can use the 
[NSNumber numberWithInt:identifier] syntax, too.

Likewise, SQL NULL values should be inserted as [NSNull 
null]. For example, in the case of comment which might be 
nil (and is in this example), you can use the comment ?: 
[NSNull null] syntax, which will insert the string if 
comment is not nil, but will insert [NSNull null] if it is nil.

翻譯成中文:

注意:基本數(shù)據(jù)類型,如NSInteger變量標(biāo)識(shí)符,應(yīng)該作為NSNumber對(duì)象,
實(shí)現(xiàn)使用@語法,如上所示。 或者你可以使用[NSNumber numberWithInt:
identifier]語法也是。

同樣,SQL NULL值應(yīng)該插入為[NSNull 空值]。 例如,在可能的評(píng)論的情況
下   nil(在這個(gè)例子中),你可以使用這個(gè)注釋嗎?[NSNull null]語法,
它將插入字符串if注釋不為零,但如果為無,則將插入[NSNull null]。

小結(jié)

對(duì)于第三方庫的使用,還是應(yīng)該通讀一下英文文檔。即使讀不懂,也可以避免走很多彎路。

2017年06月01日16:20:32

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容