How to get Sqlite database file when application is running on device?

前端 未结 8 1596
迷失自我
迷失自我 2020-12-15 01:08

I am facing problem in accessing database file when I am running it on device. How can I get that file? There is no problem in accessing file when I am running application o

8条回答
  •  时光说笑
    2020-12-15 01:54

    As others have said your database should not be within your application's root folder. If you are loading a static database from your bundle (i.e a database preloaded by adding it in xCode) you have to access it by the bundle.

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

    If you are accessing the file from the documents directory (i.e it has been modified or saved).

    NSString *fileName = @"mysqlite.sqlite";
    NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSString *path = [directoryPath stringByAppendingPathComponent:fileName];
    

提交回复
热议问题