daydr3amer
daydr3amer

Reputation: 326

App chooses a wrong database path

I use a database called db.sqlite for my iPhone App. For finding the correct path I use the following code:

- (NSString *) filePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[paths objectAtIndex:0] stringByAppendingPathComponent:@"db.sqlite"];

}

This returns the following Path:

/Users/*******/Library/Application Support/iPhone Simulator/6.0/Applications/E1D5F78D-7FD2-45D7-A3DE-B53BCBB9BDC5/Documents/db.sqlite

But the database at this path is newly created and empty. When returning the right path as a string it works well, but I don't think this will work on the iPhone.

return @"/Users/*******/workspace/xcode/MyApp/MyApp/db.sqlite";

What did I do wrong or forgot doing?

Upvotes: 0

Views: 57

Answers (1)

rmaddy
rmaddy

Reputation: 318774

The code you posted is for getting a file from the app's Documents sandbox directory. Based on the path you want, you need to get the file from the app's resource bundle.

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

Keep in mind that your db.sqlite file is read-only inside the app bundle. Do not make any attempt to write to this database file.

If you need to write to the database, you must first copy the file to a writable location (such as the Documents directory). Then use that copy of the file for all reading and writing.

Upvotes: 1

Related Questions