ios 如何删除表中的所有行?

mwg9r5ms  于 2022-11-19  发布在  iOS
关注(0)|答案(6)|浏览(149)

如何在一个步骤中删除SQLite数据库中表中的所有行(而不是逐个删除)?

ee7vknir

ee7vknir1#

执行此操作的常规SQL语法:

DELETE FROM tablename
zrfyljdw

zrfyljdw2#

SQLite Documentation 。 在 iOS 中 删除 行 :

NSString *query = @"delete from yourTable";
const char *sqlStatement = [query UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
    // Read the data from the result row
    NSLog(@"result is here");
}

// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);

中 的 每 一 个

cvxl0en2

cvxl0en23#

如果要从SQL表中删除所有行,请转到

DELETE FROM tablename

如果要逐个删除行,则转到

DELETE FROM tablename WHERE id=2

根据您的要求更改id,或者您也可以指定要删除其行的特定字段名称

wxclj1h5

wxclj1h54#

(void) DeleteRows {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath =[documentsDir stringByAppendingPathComponent:@"gym.db"];
BOOL success = [fileManager fileExistsAtPath:dbPath];
sqlite3_stmt *selectstmt;
if(!success)
{
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"gym.db"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

    if (!success)
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}

if (sqlite3_open([dbPath UTF8String], &contactDB) == SQLITE_OK) {
    //*************** insert value in database******************************\\

    NSString  *sql = [NSString stringWithFormat:@"delete from Offers"];
    const char *insert_stmt = [sql UTF8String];
    sqlite3_prepare_v2(contactDB,insert_stmt, -1, &selectstmt, NULL);
    if(sqlite3_step(selectstmt)==SQLITE_DONE)
    {
        NSLog(@"Delete successfully");
    }
    else
    {
        NSLog(@"Delete not successfully");

    }
    sqlite3_finalize(selectstmt);
    sqlite3_close(contactDB);
  }
}
zynd9foi

zynd9foi5#

- (IBAction)deleteAll:(id)sender { 

   NSString *tableName=@"Contacts";
   NSString *qsql = [NSString stringWithFormat:@"DELETE FROM %@",
                      tableName];
   sqlite3_stmt *statement;
    if (sqlite3_prepare_v2( db, [qsql UTF8String], -1,
                           &statement, NULL) == SQLITE_OK)
        while (sqlite3_step(statement) == SQLITE_DONE){
                        NSLog(@"%@", @"deleted");
        }
    else {
    sqlite3_close(db);
    NSAssert(0, @"Failed to Delete");
    }
   sqlite3_finalize(statement);

  }
l5tcr1uw

l5tcr1uw6#

在Swift中,您可以这样做:

let tableName = "myTable"
let deleteStatementString = "DELETE FROM \(tableName);"
var deleteStatement: OpaquePointer? = nil
if sqlite3_prepare_v2(db, deleteStatementString, -1, &deleteStatement, nil) == SQLITE_OK {
    if sqlite3_step(deleteStatement) == SQLITE_DONE {
        print("Successfully deleted all rowns from \(tableName)")
    } else {
        print("Could not delete all rowns from \(tableName)")
    }
} else {
    print("DELETE statement could not be prepared")
}
sqlite3_finalize(deleteStatement)

相关问题