1

我是一名 iPhone 初学者,试图从 Xcode 4 中的 sqlite 数据库中获取信息。我的数据库(名为 DB_Info.sqlite)与我的 .h 和 .m 文件位于同一目录中,并且我还拖动了数据库到 Xcode 左侧栏中的文件夹部分。您能否快速查看我的代码并让我知道我的错误在哪里?

#import "MyDatabase.h"
#import "FMDatabase.h"

    enter code here

@implementation MyDatabase


-(NSMutableArray *) OpenMyDatabase: (NSString *)query:(NSString *)column
{
    //store the database name here
    self.databaseName=@"App_Database.sqlite";

    //get the reference of the database to be stored in device or emulator
    FMDatabase *database=[self openDatabase: self.databaseName];

    //open the database
    [database open];

    //fetch a collumn from database and store it in array
   // NSArray *nsarr= [self storeDatabaseColumnInArray: database: @"select id from kas":@"id"];
    NSMutableArray *nsarr= [self storeDatabaseColumnInArray: database: query:column];

    //close the database
    [database close];
    return nsarr;

}


- (FMDatabase *)openDatabase: (NSString *)databasename
{

    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *documents_dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *db_path = [documents_dir stringByAppendingPathComponent: databasename ];
    NSString *template_path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: databasename];

    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;
}
-(void) createAndCheckDatabase
{
    BOOL success;

    NSFileManager *fileManager=[NSFileManager defaultManager];
    success=[fileManager fileExistsAtPath:_databasePath];
    NSString *databasePathFromApp=[[[NSBundle mainBundle] resourcePath]
                                   stringByAppendingPathComponent:self.databaseName ];
    [fileManager copyItemAtPath:databasePathFromApp toPath:_databasePath error:nil];

}

-(NSMutableArray *) storeDatabaseColumnInArray:(FMDatabase *)database: (NSString *)sqlStatement :(NSString *)columnName
{

    NSMutableArray *mutablearray = [NSMutableArray array];
    FMResultSet *results = [database executeQuery: sqlStatement];

    while([results next]) {

        NSString *rowValue = [results stringForColumn:columnName];

        [mutablearray addObject:results];
         NSLog(@"row value %@",rowValue);
    }

    //convert the nsmutable array to nsarray
 //   NSArray *temparr=[NSArray arrayWithArray:mutablearray];
    return mutablearray;

}

@end

不显示数据库中的数据。

无法访问数据库。

错误是

database    FMDatabase *    0x0757ed50
nsarr         NSMutableArray *  0x0718df40
4

0 回答 0