Note that there are some explanatory texts on larger screens.

plurals
  1. POReading the same record from sqlite db in iphone app
    primarykey
    data
    text
    <p>In my iphone app I use a sqlite db. I have an icon that starts the reading from db and stores it in table view. The problem is: if I select the icon again, it doubles the table view with the same record. I know the reason, because every time I select the icon the program goes to 'readSalesFromDatabase' and to 'tableView cellForRowAtIndexPath'. The problem is how to avoid this?<br> Here is my code:</p> <h2>In AppDelegate:</h2> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions { // db name databaseName = @"saSh5.sqlite"; //creating db [self checkAndCreateDatabase]; //deals will hold the retrive data deals = [[NSMutableArray alloc] init]; return YES; </code></pre> <p>}</p> <pre><code>-(void) readSalesFromDatabase { if(sqlite3_open([databasePath UTF8String], &amp;database) == SQLITE_OK) { // Setup the SQL Statement and compile it for faster access // const char *sqlStatement = "select * from UsersSale"; const char *sqlStatement = "select us.userID , us.saleStoreID, from UsersSale us order by us.saleID"; sqlite3_stmt *compiledStatement; if(sqlite3_prepare_v2(database, sqlStatement, -1, &amp;compiledStatement, NULL) == SQLITE_OK) { // Loop through the results and add them to the feeds array while(sqlite3_step(compiledStatement) == SQLITE_ROW) { // Read the data from the result row NSInteger auserID = sqlite3_column_int(compiledStatement, 0); NSInteger asalStoreID = sqlite3_column_int(compiledStatement, 1); // Create a new Sale object with the data from the database Deals *sfl = [[Deals alloc] initWithName:auserID saleStoreID:asalStoreID]; [deals addObject:sfl]; [sfl release]; } } // Release the compiled statement from memory sqlite3_finalize(compiledStatement); } sqlite3_close(database); </code></pre> <p>}</p> <h2>In Controller:</h2> <pre><code> - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.view.backgroundColor = [UIColor whiteColor]; self.title = @"Deals"; self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.myTableView.dataSource = self; self.myTableView.delegate = self; [self.view addSubview:self.myTableView]; AppDelegate *appDelegate = ( AppDelegate *)[[UIApplication sharedApplication] delegate]; [appDelegate readSalesFromDatabase]; </code></pre> <p>}</p> <pre><code>- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UILabel *product, *name; UITableViewCell *result = nil; if ([tableView isEqual:self.myTableView]){ static NSString *TableViewCellIdentifier = @"MyCells"; result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier]; if (result == nil){ result = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:TableViewCellIdentifier]; result = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier] autorelease]; AppDelegate *appDelegate = ( AppDelegate *)[[UIApplication sharedApplication] delegate]; Deals *dls = (Deals *)[appDelegate.deals objectAtIndex:indexPath.row]; result.textLabel.lineBreakMode= UILineBreakModeWordWrap; product = [[[UILabel alloc] initWithFrame:CGRectMake(25.0, 0.0, 220.0, 15.0)] autorelease]; product.tag = MAINLABEL_TAG; product.font = [UIFont systemFontOfSize:14.0]; product.textAlignment = UITextAlignmentLeft; product.textColor = [UIColor blackColor]; product.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; product.text = dls.saleSpecificProduct; [result.contentView addSubview:product]; } return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload