如何使用FMDB从表中删除所有数据

我想从我的数据库中的表中删除所有的数据。 我正在使用FMDB.And我已经使用此代码,但它不会从我的表中删除数据。

-(BOOL) deleteAll { FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]]; [db open]; BOOL success = [db executeUpdate:@"TRUNCATE TABLE customers"]; [db close]; return success; return YES; } 

尝试使用此代码。

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

只要我知道Sqlite不支持TRUNCATE查询。

尽pipeDELETE命令会起作用,但是它会很慢,因为它select每一行,然后继续删除它。

如果你正在删除整个表格,最好DROP表格并重新创build它:

 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 

斯威夫特(完整的):

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

参考: 截断SQLite