开发者

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 = NSSearchPathForDi开发者_如何学CrectoriesInDomains(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.


Update:

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜