简体   繁体   English

在SQLite中保存数组的最佳方法是什么?

[英]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.

相关问题 保存iPhone应用程序设置的最佳方法是什么? - What is the best way to save iphone app settings? 将NSMutableArray保存到NSUserDefaults的最佳方法是什么? - What is the best way to save an NSMutableArray to NSUserDefaults? 我保存和加载游戏状态的最佳方式是什么 - What is the best way for me to Save & Load gamestate 通过PHP将NSData发送到MySQL的最佳方法是什么? - What would be the best way to send NSData through PHP into MySQL? 在iPhone主屏幕上模拟动画的最佳方法是什么? - What would be the best way to simulate the animation on the iPhones home screen? 在iPhone App中提供小图像选择的最佳方法是什么? - What would be the best way of providing small image selection in an iPhone App? 在iOS中将先前创建的Sqlite数据库写入文档的最佳方法是什么 - What is the best way to write a previously created Sqlite database to documents in iOS 什么是收集iPhone sqlite db数据的最佳方法 - What is the best way to collect data for iphone sqlite db 在iPhone上保存高分以防止黑客攻击的最佳方法是什么? - What is the best way to save highscores on the iPhone to prevent hacking? iPhone UI意见-链接到通讯录中联系人的最佳方法是什么 - iphone UI opinion - what would be the best way to link to a contact in an address book
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM