如何从iPhone中的SQLite数据库中删除表数据

iPh*_*e4s 0 sqlite iphone objective-c

我正在研究SQLite数据库.我正在将数据插入到数据库中,这很好.但我想删除数据库中的数据不起作用,我想我做错了以下是我的代码,请任何人帮助我:

-(IBAction)deleteData {

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath=[documentsDir stringByAppendingPathComponent:@"paddleEight.sqlite"];
NSLog(@"The Database Path is---> %@", databasePath);

sqlite3_stmt *compiledStatement;
sqlite3 *db;
NSString *sqlStatement = @"DELETE  from GalleryTabel;";


if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) {


    if (sqlite3_prepare(db, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"record" message:@"record Deleted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];   
        [alert show];
        [alert release];
        alert=nil;
    }
    sqlite3_finalize(compiledStatement);
}
}
Run Code Online (Sandbox Code Playgroud)

iNo*_*oob 5

尝试记录NSLog(@"Error=%s",sqlite3_errmsg(&db),看看是否有任何错误.检查它是否进入内部sqlite3_open statement.

我从来没有使用分号来结束删除语句,我在sqlite中的知识非常有限.但我总是使用NSString *sqlStatement = @"DELETE from GalleryTabel";注意删除分号.

并尝试使用exec而不是prepare,step,finalize方法.

例如 :

NSString*deleteSQL=[NSString stringWithFormat:@"delete from GalleryTabel"];
const char*deleteStmt=[deleteScoreSQL UTF8String];
 char*errMsg=nil;

        if(sqlite3_exec(db, deleteScoreStmt, NULL, NULL, &errMsg)==SQLITE_OK)
        {
           NSLog(@"Deleted table");
        }
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.