how to retrieve data in a tableview
I have succeeded in adding the data to my tableview , however in the table view it will retrieve new datas and old data will not be replaced and in the table view it would retrieve 2 of the same data i have added. Meaning i would have "test, test, hi " and when i enter a new data (bye, hello) i will have "test, test, hi, test, test, hi, bye, bye, hello"
if u need additional codes tell me so thanks .
my codes :
// ---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 = [[[UIT开发者_StackOverflow中文版ableViewCell 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];
}
}
You have to add the data to the listData array after retrieving them from database.
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];
}
You are getting only rows because you have only two objects present there in the listData array.
精彩评论