导出CoreData的快速而轻松的方法是什么?

时间:2021-07-14 11:33:12

I have an app with various entities built in Core Data, which is great, but I want to extract that data so I can build a database (for research purposes).

我有一个应用程序与核心数据内置的各种实体,这是伟大的,但我想提取该数据,以便我可以建立一个数据库(用于研究目的)。

What's the quick and painless way to export CoreData entities from an iPhone?

从iPhone导出CoreData实体的快速而轻松的方法是什么?

I've been searching around and there seems to be a lot of suggestions, everything looks pretty messy. I started manually writing code to export it as CSV but that's a lot of work and if I change my database I need to change the CSV exporter to match.

我一直在寻找,似乎有很多建议,一切看起来都很混乱。我开始手动编写代码将其导出为CSV,但这是很多工作,如果我更改数据库,我需要更改CSV导出器以匹配。

There must be a really easy way to do it, no?

必须有一个非常简单的方法来做到这一点,不是吗?

Can you take the SQLite file and import it into something else? Doesn't Apple provide any APIs to export CoreData?

你能把SQLite文件导入其他东西吗? Apple不提供任何API来导出CoreData吗?

EDIT: Don't need anything fancy like exporting to a web service. I just want to export the data so I can manage it on a desktop, perhaps in Access, Excel, anything like that. It's not going to be a crazy amount of data, and this is mostly a temporary solution.

编辑:不需要像导出到Web服务那样花哨的东西。我只是想导出数据,以便我可以在桌面上管理它,也许在Access,Excel等等。它不会是一个疯狂的数据量,这主要是一个临时解决方案。

EDIT2: Found this program: http://sourceforge.net/projects/sqlitebrowser/ It's pretty much everything you need. Includes CSV export.

EDIT2:发现这个程序:http://sourceforge.net/projects/sqlitebrowser/这几乎是你需要的一切。包括CSV导出。

Thanks for your help everyone

谢谢大家的帮助

1 个解决方案

#1


2  

No, Apple doesn't provide any APIs specifically for exporting your data. You can use the existing APIs, but you have to do all the work yourself to export relationships and format the data.

不,Apple不提供任何专门用于导出数据的API。您可以使用现有的API,但您必须自己完成所有工作以导出关系并格式化数据。

If you want a quick and dirty solution, just copy the database file and use the sqlite3 command on your Mac to extract the data. Suppose the database file is named store.db. You can look at its schema using this command:

如果您想要一个快速而又脏的解决方案,只需复制数据库文件并使用Mac上的sqlite3命令来提取数据。假设数据库文件名为store.db。您可以使用以下命令查看其架构:

sqlite3 store.db .schema

You should find it very easy to figure out which tables correspond to which entities and relationships. For example, I have an entity named LibraryEntry. It's represented by this table:

您应该发现很容易找出哪些表对应于哪些实体和关系。例如,我有一个名为LibraryEntry的实体。它由此表表示:

CREATE TABLE ZLIBRARYENTRY ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT INTEGER, ZGRIDHEIGHT INTEGER, ZGRIDWIDTH INTEGER, ZDYNAMICDATARELATION INTEGER, ZORIGINALDATARELATION INTEGER, ZTHUMBNAILDATARELATION INTEGER, ZDATE TIMESTAMP, ZAUTHOR VARCHAR, ZSIGNATURE VARCHAR, ZTITLE VARCHAR );

The columns that start with Z_ are Core Data's internal metadata. Of those, you probably only care about Z_PK. The other columns (which start with Z but no underscore) are the attributes of LibraryEntry.

以Z_开头的列是Core Data的内部元数据。其中,你可能只关心Z_PK。其他列(以Z开头但没有下划线)是LibraryEntry的属性。

To dump a table in CSV, use a command like this:

要以CSV格式转储表,请使用如下命令:

sqlite3 -csv store.db 'select * from zentity' > entity.csv

If you want a header line, use the -header flag, like this:

如果你想要标题行,请使用-header标志,如下所示:

sqlite3 -header -csv store.db 'select * from zentity' > entity.csv

#1


2  

No, Apple doesn't provide any APIs specifically for exporting your data. You can use the existing APIs, but you have to do all the work yourself to export relationships and format the data.

不,Apple不提供任何专门用于导出数据的API。您可以使用现有的API,但您必须自己完成所有工作以导出关系并格式化数据。

If you want a quick and dirty solution, just copy the database file and use the sqlite3 command on your Mac to extract the data. Suppose the database file is named store.db. You can look at its schema using this command:

如果您想要一个快速而又脏的解决方案,只需复制数据库文件并使用Mac上的sqlite3命令来提取数据。假设数据库文件名为store.db。您可以使用以下命令查看其架构:

sqlite3 store.db .schema

You should find it very easy to figure out which tables correspond to which entities and relationships. For example, I have an entity named LibraryEntry. It's represented by this table:

您应该发现很容易找出哪些表对应于哪些实体和关系。例如,我有一个名为LibraryEntry的实体。它由此表表示:

CREATE TABLE ZLIBRARYENTRY ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT INTEGER, ZGRIDHEIGHT INTEGER, ZGRIDWIDTH INTEGER, ZDYNAMICDATARELATION INTEGER, ZORIGINALDATARELATION INTEGER, ZTHUMBNAILDATARELATION INTEGER, ZDATE TIMESTAMP, ZAUTHOR VARCHAR, ZSIGNATURE VARCHAR, ZTITLE VARCHAR );

The columns that start with Z_ are Core Data's internal metadata. Of those, you probably only care about Z_PK. The other columns (which start with Z but no underscore) are the attributes of LibraryEntry.

以Z_开头的列是Core Data的内部元数据。其中,你可能只关心Z_PK。其他列(以Z开头但没有下划线)是LibraryEntry的属性。

To dump a table in CSV, use a command like this:

要以CSV格式转储表,请使用如下命令:

sqlite3 -csv store.db 'select * from zentity' > entity.csv

If you want a header line, use the -header flag, like this:

如果你想要标题行,请使用-header标志,如下所示:

sqlite3 -header -csv store.db 'select * from zentity' > entity.csv