3

I'm trying to open a database that I have in my project inside Resources.

The problem is that it seems that its impossible to find the database file! I tried with the complete path, and it works, but this is not a good solution.

I would like to now how to open it!

I'm using this code: db = [FMDatabase databaseWithPath:@"bbdd.sql"];

I don't know how to find the other part of the "actual" path.

Do you have a solution for me?

Thanks!!!!

victorvj
  • 31
  • 1
  • 3

2 Answers2

8

You need to find the full path of the database in the your resource bundle, something like this :

NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"mySQLiteDatabaseFile" ofType:@"sqlite3"];

There's a complete example in this thread Copying data to the Application Data folder on the iPhone

Community
  • 1
  • 1
  • But I still have a problem it opens this long address the path it opens is: /Users/nameOfUser/Library/Application Support/iPhone Simulator/3.2/Applications/5399D73E-F31A-46B1-94B1-EAD3E2B96D6E/appName.app/bbdd.sql And the path that I need is: /Users/nameOfUser/Folder1/Folder2/appName/bbdd.sql I need to know how to get the path of the folder where the project is to open the database. – victorvj Oct 27 '10 at 14:34
  • 1
    The "long address path" is because you're running in the simulator. And it looks like the path is correct for a bundle supplied sql file. If you want to edit it, you now need to copy it to the Applications Document directory (see link in my original comment) – Jimmy Selgen Nielsen Nov 05 '10 at 11:07
4

Get Document directory:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"Test.db"];

Next

DB = [FMDatabase databaseWithPath:dbPath];
isaced
  • 1,735
  • 3
  • 16
  • 24