2010-12-22 45 views
0

我想完全清理我的iPhone應用程序中SQLite3表中的所有內容。我的MySQL經驗告訴我應該使用TRUNCATE。但似乎SQLite3不支持TRUNCATE(至少當我使用句子TRUNCATE my_table_Name準備語句時出現錯誤)。所以我轉向刪除。兩個問題:1)將DELETE FROM徹底清理我的表嗎? 2)我試了下面的代碼,它的工作。但我高度懷疑其中有不必要甚至錯誤的代碼。任何人都可以幫我看看嗎?在iPhone上的SQLite3中截斷表?

在此先感謝。

-(void)deleteAllUser { 
    NSString *ns_sql = [NSString stringWithFormat:@"DELETE FROM %@", [Config USER_TABLE_NAME]]; 
    const char *sql = [ns_sql cStringUsingEncoding:NSUTF8StringEncoding]; 
    sqlite3_stmt *statement = nil; 

    sqlite3* db = ...; //get the database instance 

    if(sqlite3_prepare_v2(db, sql, -1, &statement, nil) != SQLITE_OK) { 
     return; 
    } 

    //Without this line, table is not modified 
    int code = sqlite3_step(statement); 

    if (code == SQLITE_ROW) { 
     //Do nothing here... 
    } 

    sqlite3_finalize(statement); 
} 

回答

1

我覺得

如果(代碼== SQLITE_ROW){// 奈何這裏... }

這段代碼應該是像

if(code == SQLITE_DONE){ //此處不做任何事...... }

如果你想得到任何消息,那麼你可以從這裏返回YES,否則返回NO(如果你將返回類型從void改爲BOOL)。這可以幫助你顯示消息。

+0

謝謝Ishu,我會盡力的。 – 2010-12-22 06:37:27