Ios 当用户单击alertview时从sqlite数据库中删除记录

Ios 当用户单击alertview时从sqlite数据库中删除记录,ios,uitableview,sqlite,uialertview,Ios,Uitableview,Sqlite,Uialertview,我有一个名为FINALORDER的db表,它填充了一些数据,然后显示在tableview中 我已在didSelectRowAtIndexPath中插入了一个alertview alert=[[UIAlertView alloc]initWithTitle:@"Cancel Order" message:@"Do you want to cancel the order" delegate:self cancelButtonTitle:@"dismiss" otherButtonTitle

我有一个名为FINALORDER的db表,它填充了一些数据,然后显示在tableview中

我已在
didSelectRowAtIndexPath
中插入了一个
alertview

    alert=[[UIAlertView alloc]initWithTitle:@"Cancel Order" message:@"Do you want to cancel the order" delegate:self cancelButtonTitle:@"dismiss" otherButtonTitles:@"Ok",nil];
    [alert show];
    [alert release];
委托方法如下:

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 {
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if(buttonIndex==1)
{
    NSString *deleteSQL = @"DELETE FROM FINALORDER WHERE itemname=?"; 

    if (sqlite3_prepare_v2(database, [deleteSQL UTF8String], -1, &statement, NULL) != SQLITE_OK) 
        NSLog(@"%s error preparing %s", __FUNCTION__, sqlite3_errmsg(database)); 
    if (sqlite3_bind_text(statement, 1, [itemname UTF8String], -1, NULL) != SQLITE_OK) 
        NSLog(@"%s error binding %s", __FUNCTION__, sqlite3_errmsg(database)); 
    if (sqlite3_step(statement) != SQLITE_DONE) 
        NSLog(@"%s error executing %s", __FUNCTION__, sqlite3_errmsg(database)); 
    if (sqlite3_finalize(statement) != SQLITE_OK) 
        NSLog(@"%s error finalizing %s", __FUNCTION__, sqlite3_errmsg(database));


}
 }

我应该如何获得可以替代问号的项目名称?请注意,我的数据库由didSelectRowAtIndexPath中的以下字段组成 将名称存储在类变量中,然后使用它

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSArray *names = [cell.textLabel.text componentsSeparatedByString:@" "];
NSString *temp;
for (int i=0; i<[names count]-2; i++) 
{
   if (!temp) 
   {
         temp = [names objectAtIndex:i];
   }
   else
   {
         temp = [NSString stringWithFormat:@"%@ %@",temp, [names objectAtIndex:i]];
   }
}
self.itemName = temp;

希望能有帮助。快乐编码:)

你可以按照@AnshukGarg的建议或者我的建议去做。您可以创建自己的自定义类来处理UIAlertView

例如:

         DataAlertThing.h

            #import <Foundation/Foundation.h>

            @protocol DataAlertThingDelegate{
            @required

                -(void)shouldDeleteItem:(ItemType*)itemName;
            }

            @interface DataAlertThing : NSObject <UIAlertViewDelegate>
            @property (nonatomic, weak)id delegate;
            @property (nonatomic, strong) id itemName;

            -(id)initWithItemName:(ItemType*)myItemName;
        @end

    DataAlertThing.m

    #import "DataAlertThing.h"

    @implementation DataAlertThing



-(id)initWithItemName:(ItemType*)myItemName{
   self = [super init];
   if (self !=nil{
   self.itemName = myItemName;
  UIAlertView *alert = [UIalertVIew alloc]init...delegate:self..]
  [alert show];
}
 return self;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 {
if(buttonIndex==1)
{ 

   [delegate shouldDeleteItem:self.itemName];

 }
}
并实施以下措施:

-(void)shouldDeleteItem:(ItemType*)itemName{
 //do what you need here to delete the record

}

你为什么不把CoreData用于这个?你知道下面是一个SQLight数据库。它使用起来也简单多了,而且你最终编写的管理数据的代码也少了很多。我已经在为此自责了..哈哈..但我的项目已经走了很长一段路回首似乎不是一个选项..:-(我认为这始终是一种选择,从长远来看,它会让你的生活变得更加轻松。至少你已经定义了数据模型,因此在CoreData中实现它应该是一件轻而易举的事+有很多示例供你遵循,还有苹果公司提供的一些很好的示例代码。我对核心数据知之甚少。因此我认为我不应该在那架飞机上冒险我的时间有限无论如何,一旦我完成了最后一部分,我肯定会对它进行改造我就快完成了,这就是我不想改变的原因你的答案似乎是正确的,但我没有arrayNameToDisplayData我只有存储在数据库某处的记录我使用numericIds数组访问它们。我希望您理解我的意思。无论您以何种方式显示数据,都必须为cellForRowAtIndexPath方法中的每个单元格使用indexPath。因此,如果是arrayNameToDisplayData,您可以直接访问您的数据库。我已根据您的需要编辑了答案,这将有助于您解决此问题anks..你的答案似乎很合适..但我仍然无法删除我的元素,你的组件OperateBysting只返回我元素的一半,例如我有番茄汤..现在它只返回番茄,因此不执行删除..无论如何我会投票支持你的答案..谢谢我再次修改了它,我不知道你的项目中可能有两个单词名称,已添加。将NSArray也更改为NSMutableArray。希望这一个最终能帮助您:)谢谢你的提示我明白你的意思但是你能不能提出一些其他的建议我真的不知道你要如何传递你的myItemName注意:我的数据库中有3个字段我用一个名为numericIds的NSMutableArray来显示数据我希望你明白我的意思然后做“ItemType”id,然后向其传递一个NSMutableArray毕竟它不关心传入或传出什么,因为DataAlertThing对数据没有任何作用。只需保留一个指向它的指针,这样它就可以告诉创建它的类做什么。然后,您需要在现有类中采用DataAlertThingDelegate协议(您当前在其中创建UIAlertView的类,以便它可以响应回调并使用传递给它的数据删除记录。我已更新了答案以反映更改并修复一些相当明显的遗漏。那么,我应该如何在didSelect中检索itemname..因为我使用了一个名为numericIds的NSMutableArray来以tableview格式显示数据..您可以查看一下它pastie.org/4630043您是否可以使用recordId然后使用它检索项目。将recordId传递给DataAlertThing,然后在代理回调中以这种方式检索记录
whatever = [[DataAlertThing alloc]initWithItemName:myItemName];
[whatever setDelegate:self];
-(void)shouldDeleteItem:(ItemType*)itemName{
 //do what you need here to delete the record

}