[英]What would be the best way to save arrays in SQLite?
I'm trying to add an array into an SQLite Database on an iPhone. 我正在尝试将数组添加到iPhone上的SQLite数据库中。 The thing is, I do not know how many array objects the user will want to use.
问题是,我不知道用户想要使用多少个数组对象。 It may be 1, it may be 10. If I allocated 10 fields in a SQLite Table, that would be 9 wastes if the user was to use one, and it would put an upper bound to 10 entries.
它可能是1,它可能是10.如果我在SQLite表中分配了10个字段,那么如果用户要使用一个字段就会有9个字段,并且它会将上限设置为10个条目。
Thus I thought of using a single VARCHAR type field, and adding each object of the array with a special character between each objects. 因此,我想到使用单个VARCHAR类型字段,并在每个对象之间添加特殊字符的数组的每个对象。 How would this be performance wise?
这将如何表现明智? Any better ideas?
有更好的想法吗?
My guess will be to use the NSCoder framework. 我的猜测是使用NSCoder框架。 As NSArray conforms to NSCoding protocol, the serialization is already done to you:
由于NSArray符合NSCoding协议,因此序列化已经完成:
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:myArray forKey:myArchiveKey];
[archiver finishEncoding];
[archiver release];
// write here data object to a BLOB field of your SQLite db
Decoding will be very similar 解码将非常相似
您可以将它们保存为OBJECT列。
您可以将数组条目存储为行。
您可以将数组保存为json对象格式,同时检索将json对象转换为数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.