如何将xml解析的内容插入sqlite3数据库
我是 iPhone 开发新手。我想将 xml 解析内容插入到 sqlite3 数据库中并检索它并将其显示在 tableView 中。我能够使用 nsxml 解析器解析并直接显示内容。请帮助我如何继续存储数据库中的内容并检索它。谢谢。
I am new to iphone development.I want insert my xml parsed contents into a sqlite3 database and retrieve it and display it in a tableView.I am able to parse and directly display the contents using nsxml parser.Please help me how to proceed to store the contents in a database and retrieve it .Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该考虑使用 Core Data 而不是 sqlite3。它会自动为您管理大部分艰苦工作,并有效地加载到 TableView。
You should consider using Core Data instead of sqlite3. It manages automatically for you most of the hard works, and it loads to TableView efficiently.
您可以离线(即不在 iPhone 应用程序中)或在线(即在 iPhone 应用程序中)进行解析。这取决于您是否需要在运行时为应用程序提供 XML。
如果您只对 SQLite DB 内的 XML 文件的内容感兴趣,那么我建议您离线进行解析,因为您的应用程序运行速度会更快,因为它不会在每次启动时解析 XML。
正如 Hoang 所说,如果可能的话,您应该考虑使用 Core Data,它可以使用 SQLite 作为其后备存储。这取决于您的应用程序是否需要在 OS 3.x 之前的设备上运行,因为 Core Data 仅在 iPhone OS 3.0b 及更高版本中可用。 Core Data 使处理数据库变得更加容易,并提供了一些巧妙的小东西来使构建应用程序变得更简单(例如 NSFetchedResultsController )。
我问了类似的问题几个月前。根据我得到的回复,我编写了一个基于 Cocoa 的命令行实用程序,运行它来将数据预处理为 SQLite DB 表单,我的 iPhone 应用程序项目只是将其作为另一个资源包含在内。由于您已经在使用 NSXMLParser,我的猜测是您的大部分解析代码应该可以通过复制粘贴轻松“移植”。
You can do your parsing offline (i.e. not in the iPhone app) or online (i.e. in the iPhone app). It depends on if you need your XML available to the app at runtime.
If you're just interested in the contents of the XML file being inside the SQLite DB, then I'd recommend doing the parsing offline, because your app will run faster since it isn't parsing XML each time it's launched.
As Hoang said, you should consider using Core Data if possible, which can use SQLite for its backing store. This depends on if your app needs to run on pre-OS 3.x devices, as Core Data is only available in iPhone OS 3.0b and later. Core Data makes handling databases so much easier, and provides a few neat little things to make building an app simpler (like
NSFetchedResultsController
).I asked a similar question a few months ago. Based on the responses I got, I wrote a Cocoa-based command line utility, which I run to pre-process my data into an SQLite DB form, which my iPhone app project just includes as another resource. Since you're already using
NSXMLParser
, my guess is most of your parsing code should easily "port" over with a copy-and-paste.在使用之前,请务必阅读 Core Data 编程指南。
这不是初学者能够轻易理解的东西。
你的项目和Apple的示例代码可能有不同的情况。还要注意性能,尤其是设备上的性能。
Make sure you read Core Data Programming Guide, before you use it.
It's not something that could be easily understood by beginners.
Your project and Apple's sample codes may have different situation. Also pay attention on performance, especially on the device.