我是全新的SQLite。我有一个包含不同日子的UITableview。 (星期一至星期日)。当我点击周一例如星期一其他viewcontroller包含也在它里面的UITableview。在同一个viewcontroller我有一个UIButton,当我点击它我可以添加数据到我的SQLite数据库[A],我插入名称和星期几(星期几在这个例子'星期一',这是因为我点击星期一视图控制器)。SQLite数据显示
当我插入名称它出现在我的tableview中。但是当我回到我的第一个viewcontroller的日子,我点击例如周三我添加的数据也出现在那里。
所以我的问题是,我该怎么让我插在周一,仅在周一的tableview,而不是其他天(tableviews)
更多信息名称:
因此,当一个用户在'周一增加了一个名字我送dayoftheweek与添加的名称到SQLite数据库,当用户将在周三名称I发送 'dayoftheweek' 星期三等。
数据库咖啡看起来像=
CoffeeName | dayoftheweek
-------------------------
Hello world | Monday
Hello Planet | Wednesday
Hello Animal | Monday
Hello STOVW | Friday
[A] const char *sql = "insert into Coffee(CoffeeName, dayoftheweek) Values(?, ?)";
我需要检查,如果一天(例如)周一相同dayoftheweek(星期一),然后显示人包含“dayoftheweek周一
我的源码看起来像项目:
+ (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);
}
插入:
coffeeObj.dayoftheweek = [NSString stringWithFormat:@"%@", dayoftheweek];
此插入:monday tuesday wednesday thursday friday saturday or sunday
但我怎么能,屏幕显示T他在星期一桌面视图的星期一插入的数据和星期二控制器的星期二插入的数据等。
我试过了;
if([coffeeObj.dayoftheweek isEqualToString:@"Monday"]) {
cell.day.text = coffeeObj.LessonName;
} else {
}
显示:
- (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)
/*开始*/ 如果([CoffeeObj.dayoftheweek isEqualToString:@ “星期一”]){
// cell.Name.text = CoffeeObj.CoffeeID;
//cell.Day.text = CoffeeObj.dayoftheweek;
} else {
}
/* end */
//it need's to display in this example only things where dayoftheweek is monday but.
return cell;
}
呼叫起作用getInitialDataToDisplay
//Copy database to the user's 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]];
作为从网络移动到移动设备的人,并使用SQLite3作为我的第一个应用的支持模型,你应该考虑使用[Core Data](http://developer.apple)。com/library/ios /#DOCUMENTATION/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html)它彻底地吹走了SQLite(并将它用作其后备存储,如果需要的话),它的效率和内置效率如何。 – Josh 2012-08-17 17:19:33
我认为这个问题的问题是问题本身,它真的很难理解你想要什么。 – 2012-08-17 21:11:51
@Leandro我更新了消息 – Frenck 2012-08-17 21:45:17