Sqlite数据库表:一个数组可以成为一列记录吗?

时间:2022-12-13 13:34:16

I'm designing an iPhone app, which needs to store data into Sqlite database.

我正在设计一个iPhone应用程序,它需要将数据存储到Sqlite数据库中。

One record of the table looks like -

该表的一个记录看起来像 -

@interface Record:NSObject {
    NSInteger primaryKey;
    NSString *name;
    NSInteger priority;
    NSMutableArray *items;
};

Please be noted one column in the record is an array and the size of the array is not fixed. The size can be zero or any positive integer.

请注意,记录中的一列是一个数组,并且数组的大小不固定。大小可以是零或任何正整数。

My question is: when I design the Sqlite table, can I specify an array as one column of a record? If NO, How should I design this table?

我的问题是:当我设计Sqlite表时,我可以将数组指定为记录的一列吗?如果不是,我该如何设计这张桌子?

UPDATE: Thank you for the answer. It seems the answer to my question is NO. If so, can I use other archive methods, say, Property List or NSKeyedArchiver to implement that?

更新:谢谢你的回答。似乎我的问题的答案是否定的。如果是这样,我可以使用其他归档方法,比如Property List或NSKeyedArchiver来实现它吗?

2 个解决方案

#1


You can't do it and be normalized. Your own requirement of size "not fixed" tells you that it's impossible. This is true regardless of how you code it or which data structure you choose. It's the nature of relational databases. You'll want to read about normalization to understand it completely.

你不能这样做并且被规范化。你自己的尺寸“不固定”的要求告诉你这是不可能的。无论您如何编码或选择哪种数据结构,都是如此。这是关系数据库的本质。您需要阅读有关规范化的内容以完全理解它。

Sounds like a one-to-many foreign key relationship. You'll need one table for the Record and another for whatever object ends up in that array. The 2nd table will have a foreign key that points back to the primary key of the first.

听起来像是一对多的外键关系。你需要一个表用于记录,另一个表用于该数组中的任何对象。第二个表将有一个指向第一个主键的外键。

I would recommend a more descriptive name than "Record". It seems too generic to me.

我建议使用比“记录”更具描述性的名称。对我来说似乎太通用了。

#2


You could serialize the array to the disk and store a filename/path in the database for that column.

您可以将阵列序列化到磁盘,并在该列的数据库中存储文件名/路径。

Obviously you wont be able to query on anything in the array but...

显然你不能查询数组中的任何东西,但......

#1


You can't do it and be normalized. Your own requirement of size "not fixed" tells you that it's impossible. This is true regardless of how you code it or which data structure you choose. It's the nature of relational databases. You'll want to read about normalization to understand it completely.

你不能这样做并且被规范化。你自己的尺寸“不固定”的要求告诉你这是不可能的。无论您如何编码或选择哪种数据结构,都是如此。这是关系数据库的本质。您需要阅读有关规范化的内容以完全理解它。

Sounds like a one-to-many foreign key relationship. You'll need one table for the Record and another for whatever object ends up in that array. The 2nd table will have a foreign key that points back to the primary key of the first.

听起来像是一对多的外键关系。你需要一个表用于记录,另一个表用于该数组中的任何对象。第二个表将有一个指向第一个主键的外键。

I would recommend a more descriptive name than "Record". It seems too generic to me.

我建议使用比“记录”更具描述性的名称。对我来说似乎太通用了。

#2


You could serialize the array to the disk and store a filename/path in the database for that column.

您可以将阵列序列化到磁盘,并在该列的数据库中存储文件名/路径。

Obviously you wont be able to query on anything in the array but...

显然你不能查询数组中的任何东西,但......