我是初学的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