如何在iPhone上动态创建sqlite3数据库?

时间:2021-11-21 04:14:08

I would like to know if it is possible to create the file of a database by programming? Actually I need to create a database if it does not exist.

我想知道是否可以通过编程创建数据库文件?实际上我需要创建一个数据库,如果它不存在。

3 个解决方案

#1


1  

I'll assume you have your own valid reasons for using sqlite3 directly rather than Core Data. There are certainly cases where it's appropriate.

我假设您有自己的正当理由直接使用sqlite3而不是Core Data。肯定有适当的情况。

The sqlite_3_open() function will create the database if it doesn't already exist. The sqlite3_open_v2() function will create the database if you pass SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE in the flags parameter. See the documentation for more details.

如果数据库尚不存在,则sqlite_3_open()函数将创建数据库。如果传递SQLITE_OPEN_READWRITE,sqlite3_open_v2()函数将创建数据库。 flags参数中的SQLITE_OPEN_CREATE。有关详细信息,请参阅文档。

Of course on iPhone you'll need to make sure you're creating the database in a read-write directory such as the app's Documents directory, as opposed to the Resources directory, which is read-only.

当然,在iPhone上,您需要确保在读写目录(例如应用程序的Documents目录)中创建数据库,而不是只读资源目录。

In practice I've never tried building a database from the ground up on the iPhone. I always found it simpler to just include an empty DB file with the schema pre-built as an application resource, and then copy the file to the Documents directory the first time the app is run.

在实践中,我从未尝试过在iPhone上从头开始构建数据库。我总是发现只包含一个预先构建为应用程序资源的模式的空DB文件,然后在第一次运行应用程序时将文件复制到Documents目录。

#2


0  

Are you sure that you need to create the database file directly? Maybe you should check out the Core Data Framework.

您确定需要直接创建数据库文件吗?也许您应该查看核心数据框架。

#3


0  

Thanks for your answers, I have never used Core Data so I will have a look on this.

感谢您的回答,我从未使用过核心数据,因此我将对此进行研究。

For the moment I also copy a DB file from the Resource directory to the Documents but I would like to create a static library which can be used by many persons. So I would give a minimum of files to add to their project. That's the reason.

目前我还将一个DB文件从Resource目录复制到Documents,但我想创建一个可供许多人使用的静态库。所以我会给出最少的文件添加到他们的项目中。这就是原因。

#1


1  

I'll assume you have your own valid reasons for using sqlite3 directly rather than Core Data. There are certainly cases where it's appropriate.

我假设您有自己的正当理由直接使用sqlite3而不是Core Data。肯定有适当的情况。

The sqlite_3_open() function will create the database if it doesn't already exist. The sqlite3_open_v2() function will create the database if you pass SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE in the flags parameter. See the documentation for more details.

如果数据库尚不存在,则sqlite_3_open()函数将创建数据库。如果传递SQLITE_OPEN_READWRITE,sqlite3_open_v2()函数将创建数据库。 flags参数中的SQLITE_OPEN_CREATE。有关详细信息,请参阅文档。

Of course on iPhone you'll need to make sure you're creating the database in a read-write directory such as the app's Documents directory, as opposed to the Resources directory, which is read-only.

当然,在iPhone上,您需要确保在读写目录(例如应用程序的Documents目录)中创建数据库,而不是只读资源目录。

In practice I've never tried building a database from the ground up on the iPhone. I always found it simpler to just include an empty DB file with the schema pre-built as an application resource, and then copy the file to the Documents directory the first time the app is run.

在实践中,我从未尝试过在iPhone上从头开始构建数据库。我总是发现只包含一个预先构建为应用程序资源的模式的空DB文件,然后在第一次运行应用程序时将文件复制到Documents目录。

#2


0  

Are you sure that you need to create the database file directly? Maybe you should check out the Core Data Framework.

您确定需要直接创建数据库文件吗?也许您应该查看核心数据框架。

#3


0  

Thanks for your answers, I have never used Core Data so I will have a look on this.

感谢您的回答,我从未使用过核心数据,因此我将对此进行研究。

For the moment I also copy a DB file from the Resource directory to the Documents but I would like to create a static library which can be used by many persons. So I would give a minimum of files to add to their project. That's the reason.

目前我还将一个DB文件从Resource目录复制到Documents,但我想创建一个可供许多人使用的静态库。所以我会给出最少的文件添加到他们的项目中。这就是原因。