FMDB open database

馋奶兔 提交于 2020-01-04 02:54:32

问题


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!!!!


回答1:


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




回答2:


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];


来源:https://stackoverflow.com/questions/3880954/fmdb-open-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!