开发者

Truncate table in SQLite3 on iPhone?

I want to completely clean all the contents in a SQLite3 table in my iPhone app. My MySQL experience told me that I should use TRUNCATE. But it seems that SQLite3 doesn't support TRUNCATE (at least I got an error when preparing statement when I use the sentence TRUNCATE my_table_Name).

So I turn to DELETE FROM. Two questions: 1) Will DELETE FROM clean my table in a thorough way? 2) I tried the following code and it worked. But I highly doubted t开发者_如何学运维hat there are unnecessary or even wrong codes in it. Can anyone help me take a look at it?

Thanks in advance.

Di

-(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);
}


I think

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

this code should be like

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

If you want to get any message then you can return YES from here and NO from else(if you change return type to BOOL from void).This can help you in showing messaeges.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜