无法从SQLITE数据库中删除数据

时间:2014-01-03 10:45:51

标签: ios objective-c sqlite

我在这里做错了什么?我在数据库中有三个表,我需要从中删除所有数据。

-(void)deleteAllDataFromTables
{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSLog(@"%@", appDelegate.databasePath);

    int result = sqlite3_open([appDelegate.databasePath UTF8String], &db);
    if(result == SQLITE_OK)
    {
        NSLog(@"deleteCalled");
        const char *deleteData = "delete from CustomerNumberTable";
        int outcome = sqlite3_prepare_v2(db, deleteData, -1, &statement, NULL);

        if(outcome == SQLITE_OK)
        {
            int res = sqlite3_step(statement);
            if(res == SQLITE_DONE)
            {
                NSLog(@"deleted from Customer Table");
            }
            else
            {
                NSLog(@"not able to execute step statement");
            }
        }
        else
        {
            NSLog(@"not deleted from Customer Table");
            NSLog(@"the error is %s", sqlite3_errmsg(db));
        }
        sqlite3_finalize(statement);

        const char *deleteData2 = "delete from KeyCodeTable";
        int outcome2 = sqlite3_prepare_v2(db, deleteData2, -1, &statement, NULL);

        if(outcome2 == SQLITE_OK)
        {
            int res = sqlite3_step(statement);
            if(res == SQLITE_DONE)
            {
                NSLog(@"deleted from KeyCodeTable");
            }
            else
            {
                NSLog(@"not able to execute step statement");
            }
        }
        else
        {
            NSLog(@"not deleted from KeyCodeTable");
            NSLog(@"the error is %s", sqlite3_errmsg(db));
        }
        sqlite3_finalize(statement);

        const char *deleteData3 = "delete from SRNumberTable";
        int outcome3 = sqlite3_prepare_v2(db, deleteData3, -1, &statement, NULL);

        if(outcome3 == SQLITE_OK)
        {
            int res = sqlite3_step(statement);
            if(res == SQLITE_DONE)
            {
                NSLog(@"deleted from SRNumberTable");
            }
            else
            {
                NSLog(@"not able to execute step statement");
            }
        }
        else
        {
            NSLog(@"not deleted from SRNumberTable");
            NSLog(@"the error is %s", sqlite3_errmsg(db));
        }
        sqlite3_finalize(statement);
    }
    else
    {
        NSLog(@"Not able to open database for deleting");
    }
}

在控制台上,我正在

Application[54974:a0b] deleteCalled
2014-01-03 16:12:55.442 Application[54974:a0b] deleted from Customer Table
2014-01-03 16:12:55.443 Application[54974:a0b] deleted from KeyCodeTable
2014-01-03 16:12:55.445 Application[54974:a0b] deleted from SRNumberTable

但是当我在mac上打开数据库时,那里的数据是完整的。!!我的代码出了什么问题?为什么数据没有被删除?

4 个答案:

答案 0 :(得分:2)

我认为您正在将数据库从bundle复制到文档目录。因此,更新的数据库将出现在文档目录中。您似乎从工作区查看数据库,该数据库永远不会更新。

答案 1 :(得分:2)

您的代码会使用sqlite3_open()打开数据库,但不会使用sqlite3_close()关闭它。这意味着您的数据库可能仍处于打开事务状态,并且很可能有2个文件:一个是主database.db,而日志文件database.db-journal包含未提交的删除表的意图。

在将数据库文件从设备复制到主机之前,应确保应用程序关闭数据库。至少,将主数据库和日志文件复制为一组。它仍然可能存在一致性问题,但您应该看到您的更改。

答案 2 :(得分:2)

尝试代替sqlite3_prepare_v2sqlite3_exec。这对我来说很好。

Just Ref:

char *errMsg;
    const char *dbPath = [databasePath UTF8String];
    if (sqlite3_open(dbPath, &database) == SQLITE_OK) {
        NSLog(@"Db OPened ");
        NSString *queryLists = [NSString stringWithFormat:@"delete from Table_name"];
        const char *query_stmt = [queryLists UTF8String];
        if (sqlite3_exec(database, query_stmt,  NULL, NULL, &errMsg)
            != SQLITE_OK){
            NSLog(@"Error %s", errMsg);
            NSLog(@"Table Not Deleted");
        }

    }

答案 3 :(得分:1)

第一件事不要反复写相同的功能代码创建一个删除给定表名的所有数据的函数,

第二,确保你在mac中查看正确的sqlite文件。

看看这个,

-(void)deleteAllData
{
[self deleteTable: CustomerNumberTable];
[self deleteTable: SRNumberTable];
[self deleteTable: KeyCodeTable];


}

-(void)deleteTable:(NSString *)table;
{
    const char *dbpath=[appDelegate.databasePath UTF8String];
    if (sqlite3_open(dbpath, &dtdb)==SQLITE_OK)
    {
        sqlite3_stmt *stmt;
        const char *qry=[[NSString stringWithFormat:@"delete from %@",table] UTF8String];
        sqlite3_prepare_v2(dtdb, qry, -1, &stmt, NULL);
        if (sqlite3_step(stmt)==SQLITE_DONE)
        {
NSLog(@"%@ Table data deleted",table);            
        }
        sqlite3_finalize(stmt);
        sqlite3_close(dtdb);
    }    
}