iOS sqlite3保存數組NSArray – iPhone手機開發技術文章 iPhone軟體開發教學課程

四個註意點:
1.創建表時,聲明字段類型為二進制 blob
NSString *INEditorTableName = @"create table In_Editor(picUrls blob not null)";

2.插入sql語句,把NSArray轉換為NSData類型
NSData *dataImageUrls = [NSKeyedArchiver archivedDataWithRootObject:model.imageUrls];

3.不能用 [NSString stringWithFormat: @"sql"];
[_db executeUpdate: @"insert into In_Editor ( picUrls) values (?);",dataImageUrls];

4.讀取數據: NSArray *imageArr = [NSKeyedUnarchiver unarchiveObjectWithData:picUrls]

// 如果不註意,上面幾點,可能會出現下面錯誤:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData objectForKey:]: unrecognized selector sent to instance 0x6b46c80'

 

 

註:
NSArray 與 NSData的相互轉換 二進制
將NSArray轉化為NSData類型 :NSData *data = [NSKeyedArchiver archivedDataWithRootObject:Array];
將NSData轉化為NSArray類型 :NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];

Integer:值是signedinteger類型,大小可以是1,2,3,4,6,8bytes
REAL:浮點類型
TEXT:以UTF-8,UTF-16BEorUTF-16LE編碼存儲的字符類型
BLOB:二進制數據

 

 

You May Also Like