在tableview上需要有关更新和删除sqlite的帮助

时间:2013-01-18 07:30:34

标签: xcode sqlite ios6

我是xcode的初学者,我对此知之甚少。这是我的代码。我正在为iOS 6.0做一个简单的帐户管理程序。我需要从表视图中选择要编辑/删除数据的行,然后才能更新/删除它。我不知道如何编码,我需要经验丰富的程序员的帮助。 我的项目:http://www.mediafire.com/?95l8pwzu3c62ts0

#import "FlipsideViewController.h"

@interface FlipsideViewController ()

@end
@implementation FlipsideViewController

@synthesize entries;
- (void)viewDidLoad
{
    self.label.text=self.strUsername;
    entries = [[NSMutableArray alloc]init];
    [self openDB];
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM SUMMARY8 WHERE theuser = '%s'",[self.label.text UTF8String]];
    sqlite3_stmt *statement;
    if (sqlite3_prepare(account, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
    {
        while (sqlite3_step(statement)==SQLITE_ROW){
            char *field1=(char *) sqlite3_column_text(statement, 0 );
            NSString *field1Str = [[NSString alloc]initWithUTF8String:field1];

        char *field2=(char *) sqlite3_column_text(statement, 1 );
          NSString *field2Str = [[NSString alloc]initWithUTF8String:field2];

            char *field3=(char *) sqlite3_column_text(statement, 2 );
            NSString *field3Str = [[NSString alloc]initWithUTF8String:field3];

            char *field4=(char *) sqlite3_column_text(statement, 3 );
            NSString *field4Str = [[NSString alloc]initWithUTF8String:field4];

            NSString *str = [[NSString alloc]initWithFormat:@"%@ - %@ - %@", field1Str,field3Str,field4Str];
            [entries addObject:str];
        }
    }

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSString *) filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0]stringByAppendingPathComponent:@"account.sql"];
}
-(void) openDB
{
    if (sqlite3_open([[self filePath] UTF8String], &account) !=SQLITE_OK){
        sqlite3_close(account);
        NSAssert(0,@"Database failed to open");

    }
    else
    {
        NSLog(@"database opened");
    }
}

- (IBAction)addaccount:(id)sender {
    MainViewController *FVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MainViewController"];
    FVC.strUsername=self.strUsername;

    [self presentViewController:FVC animated:YES completion:nil];
   // FVC.label.text=self.label.text;
}

- (IBAction)btnEdit:(id)sender {
}

- (IBAction)btnDelete:(id)sender {
    [self tableView:<#(UITableView *)#> cellForRowAtIndexPath:<#(NSIndexPath *)#>];
}

#pragma mark - Actions

- (IBAction)done:(id)sender
{
    [self.delegate flipsideViewControllerDidFinish:self];
}

-(NSInteger) numberOfSectionsInTableView: (UITableView *)tableView
{
    return 1;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{
    NSString *myTitle = [[NSString alloc]initWithFormat:@"Name - Username - Password"];
    return myTitle;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      NSLog(@"%d", [entries count]);
    return [entries count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//NSLog(@"test");
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = [self.entries objectAtIndex:indexPath.row];
    return cell;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *selectedRow = [entries objectAtIndex:indexPath.row];
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM SUMMARY8 WHERE name = '%@'",selectedRow];

    char *err;
    if (sqlite3_exec(account, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK){
        sqlite3_close(account);
        NSAssert(0, @"Could not delete row");

    }
    else {
        NSLog(@"row deleted");
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Done" message:@"Account was deleted successfully!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}

@end

这是对的吗?对不起,我绝对没有Xcode的背景知识。很抱歉打扰..

1 个答案:

答案 0 :(得分:0)

删除使用此:

实施例:strUpdate = [NSString stringWithFormat:@"DELETE FROM SUMMARY8 WHERE theuser = '%s'",[self.label.text UTF8String]];

更新使用此:

示例(更新状态):strQuery = [NSString stringWithFormat:@"UPDATE SUMMARY8 SET STATUS='1' WHERE theuser = '%s'",[self.label.text UTF8String]];

希望这有帮助。

编辑: 使用这种方法:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

Example: NSString *selectedRow = [entries objectAtIndex:indexPath.row];
}

此方法基本上显示该行表的数据。操纵该数据。举个例子..根据你的情况调整它......

编辑2:

例如:

UITableView *table = [[UITableView alloc] init];
[self tableView:table didSelectRowAtIndexPath:index];

其中index是您的商品编号。