2012-10-02 49 views
0

我想從我的iPhone應用程序中的表中刪除所有行。我使用下面的代碼,但它不會刪除數據:如何從iOS應用程序的SQLite表中刪除所有內容

+(void)emptyData:(NSString*)dbPath{ 

    NSString *query = @"delete from survey_question_responses"; 
    const char *sqlStatement = [query UTF8String]; 
    sqlite3_stmt *compiledStatement; 

    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 

    // Loop through the results and add them to the feeds array 
    while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
     // Read the data from the result row 
     NSLog(@"result is here"); 
     } 

     // Release the compiled statement from memory 
     sqlite3_finalize(compiledStatement); 
    } 
    } 
+0

的NSString * QUERY1 = [的NSString stringWithFormat:@ 「從表名刪除」]; [[DBManager sharedDatabase] executeQuery:query4]; –

回答

6
NSString *query1 = [NSString stringWithFormat:@"DELETE from TableName"]; 
[[DBManager sharedDatabase]executeQuery:query1]; 

其中

-(void)executeQuery:(NSString*)_query 
{ 
    const char *sql = [_query cStringUsingEncoding:NSUTF8StringEncoding]; 
    sqlite3_stmt *statement = nil; 

    if(sqlite3_prepare_v2(dataBaseConnection,sql, -1, &statement, NULL)!= SQLITE_OK) 
    { 
     NSAssert1(0,@"error preparing statement",sqlite3_errmsg(dataBaseConnection)); 
    } 
    else 
    { 
     sqlite3_step(statement); 
    } 
    sqlite3_finalize(statement); 
} 
2

獲取數據庫的路徑:

NSString*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 

NSString*documentDirectory=[paths objectAtIndex:0]; 

NSSting*dBPath=[documentDirectory stringByAppendingPathComponent:@"dB.sql"]; 

打開DB:

if(sqlite3_open([dBPath UTF8String], &database) == SQLITE_OK){ 

    NSString*query=[NSString stringWithFormat:@"DELETE from tablename"]; 
} 

執行查詢語句:

if(sqlite3_exec(database, [query UTF8String],NULL, NULL, NULL) == SQLITE_OK){    
    NSLog(@"Delete Query Success); 

} 
else{ 

    NSLog(@"Delete Query Failed);} 

     sqlite3_close(database); 

} 

希望這有助於..

相關問題