How to remove all data from table using FMDB

牧云@^-^@ 提交于 2019-12-24 12:06:18

问题


I want to delete all data from table in my database. I am using FMDB.And i have used this code but it will not delete data from my table.

-(BOOL) deleteAll
{

    FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
    [db open];

    BOOL success =  [db executeUpdate:@"TRUNCATE TABLE customers"];

    [db close];

    return success;
    return YES;
}

回答1:


Try to use this code.

BOOL success =  [db executeUpdate:@"DELETE FROM customers"];

As long as i know Sqlite does not support TRUNCATE query.




回答2:


Although DELETE command will work it is slow because it selects each row and than proceeds to delete it.

If you are deleting the whole table it is better to DROP the table and than recreate it:

BOOL result =  [db executeUpdate:@"DROP TABLE IF EXISTS `customers`;"];
BOOL resultTwo = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS customers(name text primary key, age int)"]; //or course fields in your table will be different

Swift (for completeness sakes):

let dropTable = "DROP TABLE customers"
let result = contactDB.executeUpdate(dropTable, withArgumentsInArray: nil)
if !result {
     print("Error: \(contactDB.lastErrorMessage())")
} 
let createTable = "CREATE TABLE IF NOT EXISTS customers(name text primary key, age int)"
if !contactDB.executeStatements(createTable) {
     print("Error: \(contactDB.lastErrorMessage())")
}

reference: truncate SQLite



来源:https://stackoverflow.com/questions/15809520/how-to-remove-all-data-from-table-using-fmdb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!