2010-05-11 55 views
0

我正在使用EGODatabase來處理我的數據。當我嘗試將數據插入到使用Base創建的數據庫中並將其附加到項目中時,以重新編譯插入的數據在重新啓動應用程序時不會顯示。但是,使用Base手動插入的數據將顯示正常。EGODatabase不保留插入的記錄

以下是將數據插入到數據庫的代碼:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Database" ofType:@"db"]; 
    db = [EGODatabase databaseWithPath:path]; 
    if ([db open]) { 
     NSLog(@"Database is opened successfully!"); 
    } else { 
     NSLog(@"Database is failed to open!"); 
    } 

    NSArray *params = [NSArray arrayWithObjects:[aFeed feedID], [aFeed title], nil]; 
    [db executeUpdate:@"Insert INTO Feed (FeedID, Title) VALUES (?, ?)" parameters:params]; 
    EGODatabaseResult *results = [db executeQuery:@"SELECT * FROM Feed"]; 
    for (EGODatabaseRow * row in results) { 
     NSLog(@"ID: %@  Title: %@", [row stringForColumn:@"FeedID"], [row stringForColumn:@"Title"]); 
    } 

我很感激,如果任何人都可以分享我要EGODatabase工作示例代碼。我正在使用EGODatabase,因爲我可能有寫入數據庫的多線程代碼。 提前致謝。

回答

1

在對數據庫文件進行更改之前,您需要將數據庫文件從數據包中複製到用戶的Documents目錄。捆綁軟件資源是隻讀的,這就是爲什麼你不能更新你的數據。

把這個在您的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法:

NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Database.sqlite"]; 
NSFileManager *fileManager = [[NSFileManager alloc] init]; 
if (![fileManager fileExistsAtPath:filePath]) { 
    NSError *error = nil; 
    [fileManager copyItemAtPath:[[NSBundle mainBundle] pathForResource:@"Database" ofType:@"sqlite"] toPath:filePath error:&error]; 
} 
[fileManager release]; 
+0

謝謝你,兄弟。那真的能治好我頭疼的人:D。 – TuanCM 2010-05-11 16:27:57