How to add existent database into my app in iphone sdk

I have to add an existent DB into my iphone app I' ve tried to put it into the project's folder and used this init function

-(id) initDatabase{
    databaseName = @"mydatabase.sqlite";
    // Get the path to the documents directory and append the databaseName
 NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDir = [documentPaths objectAtIndex:0];
 databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
 [databasePath retain];

    return self;

and then i used this function

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK){ //inserting methods

but I have an error: the table 'nametable' does not exists, I am sure that this table exists and I think that these methods create a new db file into the device path.


You can't have any files in the documents folder to start. Everything that comes with the app has to be included in the app bundle. The first time the app runs, the documents folder is always empty.

You need to locate the file inside the app bundle using NSBundle's pathForResource:ofType:.

If the DB is readonly, you can just leave it there. If it is readwrite, you need to copy the file into the documents or library folder the first time the app launches.


could you make an example about using 'NSBundle's pathForResource:ofType:' please?I'm sorry but I'm new to iphone sdk

It's just:

NSString *path=[[NSBundle mainBundle] pathForResource:@"myDatabase" ofType:@"sqlite"];

In the sidebar, check "Targets" => (Your Target) => "Copy Bundle Resources". I think your database file might not have been copied because Xcode has no idea what file type that is.

Need Your Help

Generate a range in MySQL


My table layout is:

Share dom storage between http and https

javascript ssl local-storage web-storage

I would like a method of storing information on the client that can be accessed by both the SSL and nonSSL version of my site. localStorage is a great mechanism but it can only be accessed by the c...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.