删除sqlite数据库中表的所有记录

时间:2014-01-16 13:31:24

标签: ios objective-c database sqlite

我知道这是一个非常常见的问题,我尝试了我在这里找到的一切。但问题仍然存在。

现在的问题是我试图用以下代码删除3个表中的所有记录,但它们没有被删除

const char *dbpath = [dbPathString UTF8String];
sqlite3 *sqliteDB;

if (sqlite3_open(dbpath, &sqliteDB) == SQLITE_OK)
{
    //Delete all records from attempts
    sqlite3_stmt *statement;

    NSString *deleteSQL = [NSString stringWithFormat: @"DELETE FROM table1"];

    const char *delete_stmt = [deleteSQL UTF8String];
    sqlite3_prepare_v2(sqliteDB, delete_stmt, -1, &statement, NULL);

    sqlite3_finalize(statement);
    sqlite3_close(sqliteDB);
}
if (sqlite3_open(dbpath, &sqliteDB) == SQLITE_OK)
{
    //Delete all records from attempts
    sqlite3_stmt *statement;

    NSString *deleteSQL = [NSString stringWithFormat: @"DELETE FROM table2"];

    const char *delete_stmt = [deleteSQL UTF8String];
    sqlite3_prepare_v2(sqliteDB, delete_stmt, -1, &statement, NULL);

    sqlite3_finalize(statement);
    sqlite3_close(sqliteDB);
}
if (sqlite3_open(dbpath, &sqliteDB) == SQLITE_OK)
{
    //Delete all records from attempts
    sqlite3_stmt *statement;

    NSString *deleteSQL = [NSString stringWithFormat: @"DELETE FROM table"];

    const char *delete_stmt = [deleteSQL UTF8String];
    sqlite3_prepare_v2(sqliteDB, delete_stmt, -1, &statement, NULL);

    sqlite3_finalize(statement);
    sqlite3_close(sqliteDB);
}

我已尝试使用if

之类的条件
if (sqlite3_prepare_v2(sqliteDB, delete_stmt, -1, &statement, NULL) == SQLITE_OK) {
    NSLog("All records deleted");
}

这个条件得到执行,我在控制台日志中有声明。 我正在使用Xcode 5.0。 请帮我。 Thanx提前

2 个答案:

答案 0 :(得分:0)

亲爱的朋友,这有点复杂的编码尝试使用外部类来轻松编码

此链接可以帮助您

How to insert update delete in database operation and also save database in document directory

删除记录可以使用

NSString *strDelete = [Database executeScalarQuery:@"DELETE FROM table1"];

快乐编码!!!

答案 1 :(得分:0)

sqlite3_prepare_v2只是准备声明。 要实际执行它,您还必须调用sqlite3_step