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

时间:2012-10-03 05:14:36

标签: iphone xcode ipad sqlite

我使用此代码从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);
        }

我做错了什么?

3 个答案:

答案 0 :(得分:3)

嗨看看吧。

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

修改

    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

答案 1 :(得分:3)

您可以使用以下代码从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);

希望它有助于你

答案 2 :(得分:0)

是的@jason Coco是正确的,你已经准备好了两次声明,并且在准备声明之后你还没有调用步骤。在准备之后调用它:

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