Mike
Mike

Reputation:

How to dynamically create a sqlite3 database on the iPhone?

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.

Upvotes: 2

Views: 1959

Answers (3)

Mike
Mike

Reputation: 31

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.

Upvotes: 0

cduhn
cduhn

Reputation: 17918

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.

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.

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.

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.

Upvotes: 1

Nithesh Chandra
Nithesh Chandra

Reputation: 1860

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

Upvotes: 0

Related Questions