我们如何打开一个已经存在的数据库fmdb以及在哪里包含它?

我正在使用fmdb但我不知道在哪里添加db文件。 我发现这个旧的答案 ,我认为它不适合xcode 4.2(因为我们不再有’Resources’文件夹了)。

我在’Lita’中创建了一个数据库,添加了扩展名.sqlite并添加了db文件,如下所示:

dbsqlite

然后尝试使用以下内容

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docsPath = [paths objectAtIndex:0]; NSString *path = [docsPath stringByAppendingPathComponent:@"db.sqlite"]; FMDatabase *database = [FMDatabase databaseWithPath:path]; [database open]; FMResultSet *results = [database executeQuery:@"select * from tbl"]; printf("hi"); while([results next]) { printf("hi2"); NSString *name = [results stringForColumn:@"clm1"]; NSString *text = [results stringForColumn:@"clm2"]; NSLog(@"test: %@ - %@",name, text); } printf("done"); 

我正在接受’hi”完成’但从未’hi2’……

谁有人可以帮忙?

您希望将(已编译的)数据库作为资源添加到项目中,然后在首次启动时将其复制到应用程序的documents文件夹中。

下面是一个完成工作的示例方法,从applicationDidFinishLaunching调用它,并为所有查询使用生成的FMDatabase *。

 - (FMDatabase *)openDatabase { NSFileManager *fm = [NSFileManager defaultManager]; NSString *documents_dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *db_path = [documents_dir stringByAppendingPathComponent:[NSString stringWithFormat:@"database.s3db"]]; NSString *template_path = [[NSBundle mainBundle] pathForResource:@"template_db" ofType:@"s3db"]; if (![fm fileExistsAtPath:db_path]) [fm copyItemAtPath:template_path toPath:db_path error:nil]; FMDatabase *db = [FMDatabase databaseWithPath:db_path]; if (![db open]) NSLog(@"Failed to open database!"); return db; } 

我现在正在处理同样的问题。 但要回答您的问题:文档文件夹是要在设备中使用的文件夹。

将数据库放在支持文件文件夹中。

这对我有用:

 //hide status bar for full screen view [[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone]; //set reference to database here NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDir = [documentPaths objectAtIndex:0]; self.databasePath = [documentDir stringByAppendingPathComponent:@"RPRA.sqlite"]; [self createAndCheckDatabase]; // Override point for customization after application launch. return YES; } //method used to create database and check if it exists -(void) createAndCheckDatabase { BOOL success; NSFileManager *fileManager = [NSFileManager defaultManager]; success = [fileManager fileExistsAtPath:self.databasePath]; if(success) return; //else move database into documents folder NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"RPRA.db"]; [fileManager copyItemAtPath:databasePathFromApp toPath:self.databasePath error:nil]; } 
 - (void)ensureDBFile { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *dbFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"retails.sqlite"]]; NSLog(@"Database Filepath Delgate = %@", dbFilePath); NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL dbFileExists = [fileManager fileExistsAtPath:dbFilePath]; if(!dbFileExists){ //Copy the db file if it didn't exist NSString *bundledDbPath = [[NSBundle mainBundle] pathForResource:@"retails" ofType:@"sqlite"]; if (bundledDbPath != nil) { BOOL copiedDbFile = [fileManager copyItemAtPath:bundledDbPath toPath:dbFilePath error:nil]; if (!copiedDbFile) { NSLog(@"Error: Couldn't copy the db file from the bundle"); } } else { NSLog(@"Error: Couldn't find the db file in the bundle"); } } self.db = [FMDatabase databaseWithPath:dbFilePath]; [self.db setShouldCacheStatements:NO]; [self.db open]; if([self.db hadError]) { NSLog(@"Database Error %d: %@", [self.db lastErrorCode], [self.db lastErrorMessage]); } }