SQLite data showing

I am brand new with SQLite. I have a UITableview in which there are different days. (Monday to Sunday). When I click, for example, on Monday, another view node also contains a UITableview inside it. In the same viewcontroller, I have a UIButton, when I click on it, I can add data to the SQLite [ A ] database, I insert the name and day of the week (Day of the week in this example is β€œMonday”, because I clicked on the view controller on Monday).

When I insert a name , it appears in my table view. But when I return to my first view manager with days, and I, for example, click, for example, on Wednesday, the added data also appears there.

So my question is; How can I show the name that I inserted on Monday, only on Monday, and not on other days (tables)

Additional Information:

Therefore, when the user adds the name on Monday, I send the dayoftheweek with the added name to the SQLite database, when the user adds the name on Wednesday, I send the dayoftheweek on Wednesday, etc.

Coffee database looks like

CoffeeName | dayoftheweek ------------------------- Hello world | Monday Hello Planet | Wednesday Hello Animal | Monday Hello STOVW | Friday 

[A] const char *sql = "insert into Coffee(CoffeeName, dayoftheweek) Values(?, ?)";

I need to check if the day (for example) is Monday same as dayoftheweek (Monday), and then display all items containing "dayoftheweek monday"

My sqlite looks like this:

 + (void) getInitialDataToDisplay:(NSString *)dbPath { if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) { const char *sql = "select coffeeID, coffeeName from coffee"; sqlite3_stmt *selectstmt; if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) { while(sqlite3_step(selectstmt) == SQLITE_ROW) { NSInteger primaryKey = sqlite3_column_int(selectstmt, 0); Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey]; coffeeObj.LessonName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]; coffeeObj.dayoftheweek = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]; coffeeObj.isDirty = NO; [appDelegate.coffeeArray addObject:coffeeObj]; } } } else sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory. } - (void) addCoffee1 { if(addStmt == nil) { const char *sql = "insert into Coffee(CoffeeName, dayoftheweek) Values(?, ?)"; if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK) NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database)); } sqlite3_bind_text(addStmt, 1, [dayoftheweek UTF8String], -1, SQLITE_TRANSIENT); if(SQLITE_DONE != sqlite3_step(addStmt)) NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database)); else //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid LesID = sqlite3_last_insert_rowid(database); //Reset the add statement. sqlite3_reset(addStmt); } 

Insert:

 coffeeObj.dayoftheweek = [NSString stringWithFormat:@"%@", dayoftheweek]; 

this insert: monday tuesday wednesday thursday friday saturday or sunday

But how can I display the data that is inserted on Monday in the form of a table on Monday and the data that is inserted on Tuesday in the time controller, etc.

I tried;

 if([coffeeObj.dayoftheweek isEqualToString:@"Monday"]) { cell.day.text = coffeeObj.LessonName; } else { } 

Display:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CustomCellIdentifier = @"DaycusViewController"; DaycusViewController *cell = (DaycusViewController *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DaycusViewController" owner:self options:nil]; for (id oneObject in nib) if ([oneObject isKindOfClass:[DaycusViewController class]]) cell = (DaycusViewController *)oneObject; } //Get the object from the array. Coffee *coffeeObj = [appDelegate.coffeeArray objectAtIndex:indexPath.row]; cell.Name.text = CoffeeObj.CoffeeID; cell.Day.text = CoffeeObj.dayoftheweek; //i tried this: (not working) 

/ * begin * / if ([CoffeeObj.dayoftheweek isEqualToString: @ "Monday"]) {

  // cell.Name.text = CoffeeObj.CoffeeID; //cell.Day.text = CoffeeObj.dayoftheweek; } else { } /* end */ //it need to display in this example only things where dayoftheweek is monday but. return cell; } 

call to getInitialDataToDisplay function

 //Copy database to the user phone if needed. [self copyDatabaseIfNeeded]; //Initialize the coffee array. NSMutableArray *tempArray = [[NSMutableArray alloc] init]; self.coffeeArray = tempArray; [tempArray release]; //Once the db is copied, get the initial data to display on the screen. [Coffee getInitialDataToDisplay:[self getDBPath]]; 
+7
source share
3 answers

It's hard to understand your question, but I think you can better open a new project and start with Core Data. It is easy to understand, and it is faster than SQLite.

Key data is the infrastructure that Apple provides to developers, which is described as "schema-driven graph management of objects and a permanent structure." What does it mean? The structure governs where data is stored, how it is stored, data caching and memory management. It has been ported to the iPhone from Mac OS X with the release of the 3.0 iPhone SDK.

The kernel data API allows developers to create and use a relational database, perform record validation, and query using SQL-less conditions. This essentially allows you to interact with SQLite in Objective-C without having to worry about connections or managing the database schema.

More on Core Data:

I wish you the best of luck with your application, but I'm sure Core Data is the best for your application!

+1
source

If I understand correctly, your problem is not so much with SQLite, but with how to properly connect view controllers.

When the user selects one of the days of your first UITableViewController, you must pass the selection information to the next view controller. Assuming you're not using storyboards, it probably looks something like this:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:NO]; DaycusViewController *viewController = [[DaycusViewController alloc] initWithStyle:UITableViewStylePlain]; viewController.selectedDay = [days objectAtIndex:indexPath.row]; [[self navigationController] pushViewController:viewController animated:YES]; [viewController release]; } 

Since you now have information about what kind of coffee you want to display (for example, those that were on Monday) in your second view controller, you can do, for example, what Diego suggested and filter your data accordingly.

Your current approach probably doesn't work, because UITableViewDataSource methods

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

and

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

depend on each other. Therefore, you must be consistent here. The first (... cellForRowAtIndexPath ...) will be called for each row that you want to display, as you indicated in the second (... numberOfRowsInSection ...).

So, if you said that you have 4 lines to display, this will be called four times. What you cannot do is use the if statement in ... cellForRowAtIndexPath ... to simply return less than four rows. Instead, you should have a coffee collection for that particular day (ideally coming out of your data model ...). You can use the collection size for the method ... numberOfRowsInSection ... and then return the corresponding element for each row using the index path in the method ... cellForRowAtIndexPath ... as follows:

 Coffee c = [coffees objectAtIndex: indexPath.row] cell.name.text = c.name; 

There are some good examples in the Apple documentation , as well as sample code that also deals specifically with your problem.

+1
source

I'm by no means an expert on objective-c, but can this work?

 // Pass dayoftheweek to the function (void) getInitialDataToDisplay:(NSString *)dbPath:(NSString *)dayoftheweek { if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) { const char *sql = "select coffeeID, coffeeName from coffee where dayoftheweek = ?"; sqlite3_stmt *selectstmt; if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) { // Pass dayoftheweek to the query sqlite3_bind_text(addStmt, 1, [dayoftheweek UTF8String], -1, SQLITE_TRANSIENT); // Rest of the code, now the query will only return the data for the specified day 
0
source

All Articles