난 당신이 sqlite가에 대한 FMDB 목표 C 래퍼를 사용하는 것이 좋습니다. 그것은 정말 sqlite 데이터베이스에 대한 액세스를 단순화합니다. 당신은
이
당신이 액세스 앱 대리자를 사용 (다음 예제 코드를 사용하고 다른 클래스에서 DB를 액세스 할 수있는 NSString * DB_PATH 변수를 사용할 수
그런 http://code.google.com/p/flycode/source/browse/trunk/fmdb#fmdb/src
에서 다운로드 할 수 있습니다 DB_PATH, 다음 DB를 액세서하는
FMDatabase *db = [FMDatabase databaseWithPath:db_path];
를 사용합니다. 아래의 샘플 코드를 참조하십시오.
- (NSString *) initialize_db {
NSString *DATABASE_RESOURCE_NAME = @"yourDbName";
NSString *DATABASE_RESOURCE_TYPE = @"db";
NSString *DATABASE_FILE_NAME = @"yourDbName.db";
// copy the database from the bundle if necessary
// look to see if DB is in known location (~/Documents/$DATABASE_FILE_NAME)
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = [searchPaths objectAtIndex: 0];
NSString *dbFilePath = [documentFolderPath stringByAppendingPathComponent: DATABASE_FILE_NAME];
[dbFilePath retain];
if (! [[NSFileManager defaultManager] fileExistsAtPath: dbFilePath]) {
// didn't find db, need to copy
NSString *backupDbPath = [[NSBundle mainBundle]
pathForResource:DATABASE_RESOURCE_NAME
ofType:DATABASE_RESOURCE_TYPE];
if (backupDbPath == nil) {
// couldn't find backup db to copy, bail
NSLog (@"couldn't init db");
return NULL;
} else {
BOOL copiedBackupDb = [[NSFileManager defaultManager]
copyItemAtPath:backupDbPath
toPath:dbFilePath
error:nil];
if (! copiedBackupDb) {
// copying backup db failed, bail
NSLog (@"couldn't init db");
return NULL;
}
}
}
return dbFilePath;
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
FMResultSet *item_rs;
// copy the database from the bundle if necessary
db_path = [self initialize_db];
if (! db_path) {
// TODO: alert the user!
NSLog (@"couldn't init db");
return;
}
FMDatabase *db = [FMDatabase databaseWithPath:db_path];
if (![db open]) {
NSLog(@"Could not open the db");
}
FMResultSet *rs = [db executeQuery:@"select * from yourTable"];
if ([db hadError]) {
NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
}
while ([rs next]) {
[yourArray addObject:[rs stringForColumn:@"yourColumnName"]];
}
[rs close];
[db close];
// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}