如何在tableview中检索数据

时间:2011-09-07 07:09:23

标签: iphone tableview

我已成功将数据添加到我的tableview,但是在表视图中它将检索新数据并且旧数据不会被替换,并且在表视图中它将检索我添加的2个相同数据。意思是我会“测试,测试,嗨”,当我输入新数据(再见,你好)时,我会“测试,测试,嗨,测试,测试,嗨,再见,再见,你好”

如果你需要额外的代码,请告诉我,谢谢。

我的代码:

       // ---Retrieve Rows---
    -(void) getAllRowsFromTableNamed: (NSString *) tableName {
        [self openDB];
        NSString *qsql = @"SELECT * FROM Favourites";
        sqlite3_stmt *statement;

        if (sqlite3_prepare_v2(db, [qsql UTF8String], -1, &statement, nil) ==
            SQLITE_OK) {
            // Setup the SQL Statement and compile it for faster access
            while (sqlite3_step(statement) == SQLITE_ROW)
            {

                // Read the data from the result row
                char *field1 = (char *) sqlite3_column_text(statement, 0);
                self.field1Str = [[NSString alloc] initWithUTF8String: field1];

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


                NSString *stringValue = [[NSString alloc] initWithUTF8String: field1];
                [self.listArray addObject:stringValue];
                [stringValue release];

                NSLog(@"Address : %@ Description : %@", self.field1Str, self.field2Str);

            }
        }
        // Release the compiled statement from memory
        sqlite3_finalize(statement);
        sqlite3_close(db);
        NSLog(@"DATA RETRIEVED");
    }
    //---End of Retrieve Row---



        - (NSInteger)tableView:(UITableView *)tableView
         numberOfRowsInSection:(NSInteger)section
        {
            return [self.listData count];
        }

        - (UITableViewCell *)tableView:(UITableView *)tableView
                 cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            static NSString *TableIdentifier = @"TableIdentifier";
            UITableViewCell *cell = [tableView
                                     dequeueReusableCellWithIdentifier:TableIdentifier];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                              reuseIdentifier:TableIdentifier] autorelease];
            }

            NSUInteger row = [indexPath row];
            cell.textLabel.text = [listData objectAtIndex:row];
            return cell;

        }

    - (void) viewWillAppear:(BOOL)animated {
        [self getAllRowsFromTableNamed:@"Favourites"];

        NSArray *array = [[NSArray alloc] initWithObjects:self.field1Str, self.field2Str, nil];

        [self.listArray addObjectsFromArray:array];
        [array release];
        [favouriteListTableView reloadData];
        //NSLog(@"String1:%@, String2:%@", self.string1, self.string2); 
    }
============================================================================================
-(void) insertRecordIntoTableNamed: (NSString *) tableName
                        withField1: (NSString *) field1 field1Value: (NSString *) field1Value
                         andField2: (NSString *) field2 field2Value: (NSString *) field2Value {
    [self openDB];
    NSString *sql = [NSString stringWithFormat:
                     @"INSERT OR REPLACE INTO '%@' ('%@', '%@') VALUES ('%@','%@')",
                     tableName, field1, field2, field1Value, field2Value];

    char *err;
    if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK)
    {
        sqlite3_close(db);
        NSAssert(0, @"Error updating table.");

    }
    NSLog(@"DATA RECORDED");

}

-(IBAction)doneButton:(id)sender {
    if([addressTextField.text isEqualToString:@""] || [descriptionTextField.text isEqualToString:@""]){
        UIAlertView *alert = [[UIAlertView alloc] 
                              initWithTitle:@"Invalid Input!" 
                              message:@"Please enter Address or Description"
                              delegate:self 
                              cancelButtonTitle:@"OK" 
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    else{   
    [self insertRecordIntoTableNamed:@"Favourites"
                          withField1:@"address" field1Value:addressTextField.text
                           andField2:@"description" field2Value:descriptionTextField.text]; 

    NSArray *viewControllers = [self.navigationController viewControllers];

    FavouriteListViewController *favouriteListVC = (FavouriteListViewController *)[viewControllers objectAtIndex:viewControllers.count - 2];

    [favouriteListVC setString1:self.field1Str];
    [favouriteListVC setString2:self.field2Str];
    [self.navigationController popViewControllerAnimated:YES];
    }
}

1 个答案:

答案 0 :(得分:0)

从数据库中检索数据后,必须将数据添加到listData数组中。

 while (sqlite3_step(statement) == SQLITE_ROW)
        {
            // Read the data from the result row
            char *field1 = (char *) sqlite3_column_text(statement, 0);
            NSString *stringValue = [[NSString alloc] initWithUTF8String: field1];
            [self.listData addObject:stringValue];
            [stringValue release];

        }

您只获得行,因为listData数组中只有两个对象。