Objective-C 和 RDBMS(如 SQLite)之间的交互是什么?

发布于 2024-10-11 17:43:11 字数 571 浏览 8 评论 0原文

我想学习如何从 Objective-C 将数据持久保存到 RDBMS,但我真的不知道从哪里开始学习。我要学习 RDBMS 吗?我学习数据建模吗?

我想知道在对数据进行建模时是否有一些技术或特殊考虑因素,以免遇到任何陷阱?即是否有诸如“不要子类化”或“始终封装您的属性”之类的经验法则。

根据我有限的经验,将 Objective-C 类转换为关系数据库是相当困难的。看起来 CoreData 可能会让我走上正确的道路,但 CoreData 似乎也给了我很多理所当然的东西(我很好奇 SQL 调用的幕后情况) ...)。还是我对这个框架的理解错误?

我正在寻找任何可以帮助我更好地理解 RDBMS 以及 Objective-C 模型类通常如何与它们交互以进行数据存储的资源。

编辑:

为了回答我自己的好奇心,我学习了 Joe Celko 的 SQL for Smarties 以及 Clare Churcher 的 Beginning Database Design。它们都没有真正提供用非 SQL 语言(在我的例子中是 Objective-C)编写的控制器类、SQL 和数据库之间的交互。有一个缺失的链接,我只是不明白......

I would like to learn how to persist data to a RDBMS from Objective-C, and I don't really know where to start to learn this. Do I learn a RDBMS? Do I learn data modeling?

I'm wondering are there techniques or special considerations when modeling the data as to not run into any pitfalls? I.e. are there rules of thumb like "don't subclass" or "always encapsulate your attributes."

In my limited experience it has been quite difficult to translate an Objective-C class into a relational database. It would seem that CoreData might get me started off on the right path, but it also seems like CoreData kinda just gives me a lot of things to take for granted (I'm curious to know what's going on under the hood with the SQL calls...). Or am I understanding this framework wrong?

I'm looking for any resources that would get me started down the path of better understanding RDBMSes and how Objective-C model classes typically interact with them for data storage.

EDIT:

In an effort to answer my own curiosity, I've picked up Joe Celko's SQL for Smarties as well as Beginning Database Design by Clare Churcher. Neither of them really give much by way of the interaction between controller classes written in non-SQL languages (in my case Objective-C), SQL, and the database. There's a missing link that I'm just not understanding...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

中性美 2024-10-18 17:43:11

抱歉这么久才回来找你。你所问的并不是 Objective-C 特有的。我第一次介绍将面向对象代码连接到 RDBMS 是 NextStep 中的企业对象框架。但从那时起,这个想法就被大多数面向对象的语言所复制,包括 Java 和 Ruby(参见 ActiveRecord)。

从概念上讲,在编程方面通常有一个实体类用于表示表的每一行。在某些情况下,例如 CoreData 或 WebObjects,映射用于创建应用程序代码和数据库之间的接口。由于此映射,开发人员可以使用通用实体类的实例来表示数据。当然,很多时候该类会被子类化以添加特定于特定实体的方法。

例如,假设您有一个联系人表,其中有一列表示名字,一列表示姓氏。通常在应用程序中您希望显示全名。在实体类的子类中,可以添加一个以单个字符串形式返回名字和姓氏的方法。

在其他框架中,例如ActiveRecord,我相信您必须始终有一个代表每个表的子类。

从概念上讲,我发现面向对象编程与 RDBMS 非常契合。

表(联系人)->类(联系人)

行 ->类 (aContact) 的实例 列

(firstName) ->属性(又名实例变量、属性)(firstName)

关系:

to-one (father) -> Properties (father, an instance of Contact)

to-many (emailAddresses) -> Array (emailAddresses, an array of instances of EmailAddress class)

希望这能更好地回答您的问题,

Sorry it's taken so long to come back to you. What you are asking is not specific to Objective-C. My first introduction of connecting Object-oriented code to RDBMS was Enterprise Object Frameworks in NextStep. But since then, that idea has been copied in most object-oriented languages including Java and Ruby (see ActiveRecord).

Conceptually, on the programming side there is usually a entity class that is used to represent each row of a table. In some cases, such as CoreData or WebObjects, a map is used to create an interface between the application code and the database. Because of this map, a developer can use instances of the generic entity class to represent the data. Of course, many times that class is subclassed to added methods specific to a particular entity.

For example, say you have a table for contacts, which has a column for first name and a column for last name. Often in an application you want to display the full name. In a subclass of the entity class, one can add a method that returns the first and last name as a single string.

In other frameworks, such as ActiveRecord, I believe you must always have a subclass that represents each table.

Conceptually, I find Object-Oriented programming to align well with RDBMS.

Table (contacts) -> Class (Contact)

Row -> Instance of class (aContact)

Columns (firstName) -> Properties (aka instance variables, attributes) (firstName)

Relationships:

to-one (father) -> Properties (father, an instance of Contact)

to-many (emailAddresses) -> Array (emailAddresses, an array of instances of EmailAddress class)

Hope this answers your question better,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文