xcode 无法从 iOS 中的 SQLite 数据库中删除行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12702266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:52:35  来源:igfitidea点击:

Can't delete row from SQLite database in iOS?

iphonexcodeipadsqlite

提问by Nitin Gohel

I am using this code to delete row from Sqlite3 Database,but it's not working,insert,update,select query are working fine, but the delete statement is not working in my code

我正在使用此代码从 Sqlite3 数据库中删除行,但它不起作用,插入、更新、选择查询工作正常,但删除语句在我的代码中不起作用

 -(void)delete{

        str=title.text;
        sqlite3_stmt *selectstmt;
        if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
            NSString *deleteSQL = [NSString stringWithFormat: @"delete from ToDo where title=\"%@\"",str];
            NSLog(@"%@",deleteSQL);
            const char *sql = [deleteSQL UTF8String];
            sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL); 
            if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) 
               NSLog(@"success");
            else
              NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(database));            
            }
            sqlite3_finalize(selectstmt);
            sqlite3_close(database);
        }

What am I doing wrong?

我究竟做错了什么?

回答by iLearner

Hi take a look at it.

你好,看看吧。

I thing you are doing wrong in passing the parameter.

我认为你在传递参数时做错了。

EDIT:

编辑:

    str=title.text;
    [self openDatabase];//Logic to open database goes here
    sqlite3_stmt *stmt=nil;
    BOOL status=FALSE;

    NSString *deleteSQL = [NSString stringWithFormat: @"delete from ToDo where title='%@'",str];
    NSLog(@"%@",deleteSQL);
    const char *sql = [deleteSQL UTF8String];


   //const char *sql ="delete from ToDo where title='%@'";

   if(sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK)
   {
         sqlite3_bind_text(stmt, 1, [_testId UTF8String], -1, SQLITE_STATIC);

         if(SQLITE_DONE != sqlite3_step(stmt))
               { 
                 NSLog(@"Error while Deleting");
               }


    }
    sqlite3_finalize(stmt);
  [self closeDatabase];//Logic to close database goes here

回答by Nitin Gohel

you can delete particular recored from sqlite database using below code:-

您可以使用以下代码从 sqlite 数据库中删除特定记录:-

NSDictionary *d =(NSDictionary *) [YourMutableArray objectAtIndex:0];
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [path objectAtIndex:0];
NSString *dbPath =[documentsDir stringByAppendingPathComponent:@"db.sqlite"];
sqlite3_open([dbPath UTF8String], &database);

if(deleteStmt == nil) 
{

    NSString *deleteStatementNS = [NSString stringWithFormat:
                                   @"delete from tbl_postoffice where type = '%@'",
                                   urusniqIDentifire];
    const char *sql = [deleteStatementNS UTF8String];

    //const char *sql ="delete from tbl_todo where type = yourDbfildeRecored";
    if(sqlite3_prepare_v2(database, sql, -1, &deleteStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(database));
}

//When binding parameters, index starts from 1 and not zero.
sqlite3_bind_text(deleteStmt, 1, [[d valueForKey:@"type"] UTF8String], -1, SQLITE_TRANSIENT);
if (SQLITE_DONE != sqlite3_step(deleteStmt)) 
    NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database));

sqlite3_reset(deleteStmt);

hope its Helps for you

希望它对你有帮助

回答by Nuzhat Zari

Yes @jason Coco is correct you have prepared your statement twice and you have not called step also after prepare statement..try calling this after prepare:

是的@jason Coco 是正确的,您已经准备了两次您的语句,并且您还没有在准备语句之后调用 step.. 尝试在准备之后调用它:

if (sqlite3_step(selectstmt) == SQLITE_DONE)
{
    NSLog(@"success");
}