2010-08-27 175 views
1

我的應用程序中存在持久性問題。我正在使用sqlite數據庫。當一些插入查詢執行結果臨時添加到數據庫時。重新啓動應用程序後,新值將消失!我認爲存儲在RAM上的新值不會保存在硬盤上。Xcode Sqlite持久性問題

-(IBAction)add:(id)sender 
{ 

NSString *myDB; 
NSString *query; 
myDB=[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Ssozluk.sql"]; 
database =[[Sqlite alloc] init]; 
[database open:myDB]; 
query=[NSString stringWithFormat:@"INSERT INTO words(col_1,col_2) VALUES('asd','asd2');"]; 

[database executeNonQuery:query]; 
[database commit]; 
[database close]; 

} 

-(IBAction)show:(id)sender 
    { 
    NSString *myDB; 
    NSString *query; 
    NSArray *asdasd; 
    NSString *asd; 
    myDB=[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Ssozluk.sql"]; 
    database =[[Sqlite alloc] init]; 
    [database open:myDB]; 
    query=[NSString stringWithFormat:@"Select col_2,col_1 FROM words"]; 

    asdasd=[database executeQuery:query]; 
    for(NSDictionary *row in kelimeler) 
    { 
    asd=[row valueForKey:@"col_2"]; 
    olabel1.text=asd; 
    } 


    [database commit]; 
    [database close]; 

    } 

回答

0

你需要編程數據庫複製到Documents目錄的應用,並與可寫副本。包中的資源是隻讀的。

+0

我正在使用Xcode iPhone應用程序。我將我的數據庫複製到xcode中的資源文件夾。但仍然不工作。 – estergones 2010-08-27 08:15:56

+0

xcode中的Documents文件夾在哪裏?我如何複製和使用可寫副本? – estergones 2010-08-27 08:27:48

+0

這是應用程序中的文檔目錄。看看這裏 - http://stackoverflow.com/questions/272544/whats-the-best-way-to-find-the-users-documents-directory-on-an-iphone – Liam 2010-08-27 08:34:17

0

我用下面的代碼到我的數據庫複製到文件夾,比獲得可寫的部分:

- (void)createEditableCopyOfDatabaseIfNeeded { 
// First, test for existence. 
BOOL success; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"]; 
//NSLog(writableDBPath); 
success = [fileManager fileExistsAtPath:writableDBPath]; 
if (success) return; 
// The writable database does not exist, so copy the default to the appropriate location. 
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sqlite"]; 
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
if (!success) { 
    NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
} 

}

- (void)initializeDatabase { 
// The database is stored in the application bundle. 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"]; 
// Open the database. The database was prepared outside the application. 
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) { 
    //Add initial sql statements here 

} else { 
    // Even though the open failed, call close to properly clean up resources. 
    sqlite3_close(database); 
    NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database)); 
    // Additional error handling, as appropriate... 
} 

}

- (sqlite3*) getDB{ 
return database; 

}

將c ALLS您applicationDidFinishLaunching方法

0

至於我還記得,當您使用路徑DB你的方式,它應該被添加到項目。

0

有時候,特別是數據庫,只是清潔不起作用!

轉到目錄:/用戶/ qjsi /庫/應用程序支持/ iPhone模擬器/ 4.3 /應用

在那裏,你會發現你所運行的所有項目。找到包含您正在處理的項目的文件夾,並將其刪除。然後通過Xcode清理你的項目,然後運行。該目錄中的文件夾將被重新創建,數據庫也將被重新創建。

注意:數據庫也將被刪除!如果你把它保存在你的軟件包中,並將其複製到可編輯的目錄中,請注意數據庫將與你的軟件包中的數據庫相同(因此,iPhone模擬器中沒有修改記錄)。