如何检索表视图中的数据
我已成功将数据添加到我的 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];
}
}
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 = [[[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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从数据库检索数据后,您必须将数据添加到 listData 数组中。
您只获得行,因为 listData 数组中只有两个对象。
You have to add the data to the listData array after retrieving them from database.
You are getting only rows because you have only two objects present there in the listData array.