首页 > 代码库 > iOS sqlite3保存数组NSArray

iOS sqlite3保存数组NSArray

四个注意点:
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:二进制数据




参考:

http://www.4byte.cn/question/22141/saving-nsarray-in-sqlite-and-retrieving-it.html

http://www.chengxuyuans.com/iPhone_IOS/37789.html


iOS sqlite3保存数组NSArray