简体   繁体   English

[NSBundle mainBundle] pathForResource返回nil

[英][NSBundle mainBundle]pathForResource returns nil

I'm trying to connect my app to SQLite database but I get messages that tell me that the database is created but there are no tables. 我正在尝试将我的应用程序连接到SQLite数据库,但收到的消息告诉我已创建数据库,但没有表。

I debugged the program and discovered that [[NSBundle mainBundle] pathForResource] always returns nil 我调试了程序,发现[[NSBundle mainBundle] pathForResource]始终返回nil

NSString* file = [[NSBundle mainBundle]pathForResource:_databaseName ofType:_databaseType];

Why is this happening? 为什么会这样呢? Please help me! 请帮我!

First : make sure your database is in the app bundle. 首先:确保您的数据库位于应用程序捆绑包中。

Then you need to implement methods to copy the path to the documentary Directory .as these 然后,您需要实现将路径复制到记录目录的方法。

-(NSString *) getDBPath:(NSString*)databaseName {
// search for the existed paths
NSArray *paths = NSSearchPathForDirectoriesInDomains(  NSDocumentDirectory , NSUserDomainMask, YES);
  NSString *documentsDir = [paths objectAtIndex:0];


return [documentsDir stringByAppendingPathComponent:databaseName];

} }

- (void) InstantiateYourDatabase:(NSString *)DatabaseName {


//Using NSFileManager we can perform many file system operations.

NSFileManager *fileManager = [NSFileManager defaultManager];

NSError *error;
NSString *dpname=DatabaseName;

_dbPath = [self getDBPath:dpname];

BOOL success = [fileManager fileExistsAtPath:_dbPath];

NSLog(@"success == %i",success);

if( !success) {

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] 
                               stringByAppendingPathComponent:DatabaseName];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:_dbPath error:&error];

}  

} }

Tell me if it helps! 告诉我是否有帮助!

try below code for find database path 尝试下面的代码来查找数据库路径

    NSString *databaseName = @"your_db_name.sql";
    NSString *databasePath;
    // Get the path to the documents directory and append the databaseName
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    NSLog(@"Database Path is: %@",databasePath);

You want More Detail about Sqllite Database Connectivity in iOS try this tutorial 您想要有关iOS中Sqllite数据库连接的更多详细信息,请尝试本教程

Welcome! 欢迎!

If you created a sqlite db file and drag it to your project in xcode3, it's in application document folder while its running. 如果创建了sqlite db文件并将其拖动到xcode3中的项目中,则该文件在运行时位于应用程序文档文件夹中。

NSString *databaseName = @"cartoon.db";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentDir stringByAppendingPathComponent:databaseName];

每当使用数据库时,都必须使用文档目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM