2013-06-12 82 views
3

我開發了SQLite數據庫,當應用程序「didFinishLoading」數據庫已清除並調用Web服務以獲取數據並將其插入到數據庫中。它在所有iPhone和iPad設備上工作正常,但在iPhone5上,從數據庫中刪除數據時會崩潰。當從iPhone5中的SQLite中刪除數據時,應用程序崩潰

這裏,tableNamesArray表示SQLite中的表。

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
{ 
    for (int i = 0; i < [tableNamesArray count]; i++) 
    { 
     NSString *querySQL = [NSString stringWithFormat:@"DELETE FROM %@", [tableNamesArray objectAtIndex:i]]; 

     NSLog(@"querySQL %@", querySQL); 

     // SELECT QuationID,Quation FROM QuationsTable WHERE GroupID="Group0" 

     const char *query_stmt = [querySQL UTF8String]; 
     sqlite3_stmt *compiledStatement; 
     if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &compiledStatement, NULL) == SQLITE_OK) 
     { 
     } 

     if (sqlite3_step(compiledStatement) != SQLITE_DONE) 
     { 
     } 
     else 
     { 
     } 

     sqlite3_finalize(compiledStatement); 
    } 

    sqlite3_close(contactDB); 
} 

請幫幫我。

+1

你試圖更新應用程序包中的文件的任何機會? – 2013-06-12 12:50:13

+0

yes @ H2CO3,我改變了Dispaly Name – Ravi

+0

mr.H2C03,第一次當我爲sqlite添加3個表格創建DB文件後,我刪除了該文件並添加了一些表格到同一個文件。再次添加到應用程序。 – Ravi

回答

0

使用此功能將您的數據庫複製到緩存目錄中。

-(void)createDBcopy:(NSString*)fileName 
{ 
    // Check if the SQL database has already been saved to the users phone, if not then copy it over 
    BOOL success; 

    // Create a FileManager object, we will use this to check the status 
    // of the database and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *DBPath=[documentsDirectory stringByAppendingPathComponent:fileName]; 
    //DBPath = [DBPath stringByAppendingString:@".sqlite"]; 
    // Check if the database has already been created in the users filesystem 
    success = [fileManager fileExistsAtPath:DBPath]; 

    // If the database already exists then return without doing anything 
    if(success) return; 

    // If not then proceed to copy the database from the application to the users filesystem 
    // Get the path to the database in the application package 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName]; 

    // Copy the database from the package to the users filesystem 
    [fileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil]; 

    [fileManager release]; 

} 

而且使用它作爲:

[yourClassObjectWhereTheMethodIsWritten createDBcopy:@"yourDB.sqlite"]; 

你應該didFinishLaunchWithOption在AppDelegate中使用它。

相關問題