简体   繁体   English

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

[英]How to delete a table data from SQLite Database in iPhone

I am working on a SQLite database. 我正在使用SQLite数据库。 I am inserting data into database that's fine. 我可以将数据插入数据库中。 But I want to delete data from database its not working, I think I am doing wrong some where following is my code please any one help me: 但是我想从数据库中删除数据无法正常工作,我想我在以下某些地方做错了我的代码,请任何人帮助我:

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

Try logging NSLog(@"Error=%s",sqlite3_errmsg(&db) , see if there are any errors. Check if it enters inside the sqlite3_open statement . 尝试记录NSLog(@"Error=%s",sqlite3_errmsg(&db) ,看是否有任何错误。检查它是否进入sqlite3_open statement

And i've never used semicolon to end the delete statement, my knowledge in sqlite is very limited. 而且我从未使用过分号来结束删除语句,我在sqlite中的知识非常有限。 But i've always used NSString *sqlStatement = @"DELETE from GalleryTabel"; 但我一直使用NSString *sqlStatement = @"DELETE from GalleryTabel"; Note the semicolon is removed. 请注意,分号已删除。

And try using exec instead of prepare,step,finalize method. 并尝试使用exec而不是prepare,step,finalize方法。

For example : 例如 :

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM