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