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

时间:2012-08-31 08:11:10

标签: ios uitableview sqlite uialertview

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

我在alertview ..

中插入了didSelectRowAtIndexPath
    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));


}
 }

我该如何获取可替换问号的项目名称?请注意,我的数据库包含以下字段itemname,quantity,totalcost

2 个答案:

答案 0 :(得分:1)

在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;

并使用它

    NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM FINALORDER WHERE itemname=%@",self.itemName];
希望它有所帮助。快乐的编码:)

答案 1 :(得分:1)

你可以做@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];

 }
}

希望从上面你可以得到关于我建议的内容的足够的想法 - 你将警报逻辑封装在你自己的自定义类中。你也可以在这一点上使用一个块,但我认为今天就有一个例子就足够了:)

请注意此ARC并使用现代约定,因此您无需更正@synthesize

在您初始化警报视图的现有代码中。请改为初始化此自定义类。

whatever = [[DataAlertThing alloc]initWithItemName:myItemName];
[whatever setDelegate:self];

并实施以下

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

}