简体   繁体   中英

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. 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.

Thus I thought of using a single VARCHAR type field, and adding each object of the array with a special character between each objects. How would this be performance wise? Any better ideas?

My guess will be to use the NSCoder framework. As NSArray conforms to NSCoding protocol, the serialization is already done to you:

  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对象转换为数组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM