SQLite数据显示

Fre*_*nck 7 sql sqlite cocoa-touch objective-c ios

我是SQLite的新手.我有一个UITableview包含不同的日子.(星期一至星期日).当我点击例如星期一时,另一个viewcontroller包含一个UITableview.在同一个viewcontroller我有一个UIButton当我点击它我可以添加数据到我的SQLite数据库[ A ],我插入名称和星期几(星期几是在这个例子'星期一'这是因为我点击星期一视图控制器).

当我插入一个名字时,它出现在我的tableview中.但是当我回到我的第一个视图控制器时,我在周三点击了例如我添加的数据也出现在那里.

所以我的问题是; 如何显示我在星期一插入的名称,仅在星期一的tableview中,而不是其他日子(tableviews)

更多信息:

因此,当用户在'星期一'添加名称时,我将带有添加名称的dayoftheweek发送到SQLite数据库,当用户在星期三添加名称时,我发送'dayoftheweek'星期三等.

数据库咖啡看起来像=

CoffeeName    | dayoftheweek
-------------------------
Hello world   | Monday
Hello Planet  | Wednesday
Hello Animal  | Monday
Hello STOVW   | Friday
Run Code Online (Sandbox Code Playgroud)

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

我需要检查一天(例如)星期一是否与dayoftheweek(星期一)相同,然后显示包含'dayoftheweek monday'的项目

我的sqlite看起来像:

+ (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);
}
Run Code Online (Sandbox Code Playgroud)

插入:

coffeeObj.dayoftheweek = [NSString stringWithFormat:@"%@", dayoftheweek];
Run Code Online (Sandbox Code Playgroud)

这个插入: monday tuesday wednesday thursday friday saturday or sunday

但是如何显示星期一tableview中星期一插入的数据和星期二控制器等周二插入的数据.

我试过了 ;

if([coffeeObj.dayoftheweek isEqualToString:@"Monday"]) {

cell.day.text = coffeeObj.LessonName;


} else {

}
Run Code Online (Sandbox Code Playgroud)

显示:

- (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)
Run Code Online (Sandbox Code Playgroud)

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

        //  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;
}
Run Code Online (Sandbox Code Playgroud)

调用函数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]];
Run Code Online (Sandbox Code Playgroud)

Bla*_*zer 1

很难理解你的问题,但我认为你可以更好地打开一个新项目并从核心数据开始。它很容易理解并且比 SQLite 更快。

\n\n

Core Data 是 Apple 向开发人员提供的一个框架,被描述为 \xe2\x80\x9cschema 驱动的对象图管理和持久性框架。\xe2\x80\x9d 这实际上意味着什么?该框架管理数据的存储位置、存储方式、数据缓存和内存管理。它通过 3.0 iPhone SDK 版本从 Mac OS X 移植到 iPhone。

\n\n

Core Data API 允许开发人员创建和使用关系数据库、执行记录验证以及使用无 SQL 条件执行查询。它本质上允许您与 Objective-C 中的 SQLite 交互,而不必担心连接或管理数据库模式

\n\n

有关核心数据的更多信息:

\n\n\n\n

祝您申请顺利,但我确信 Core Data 最适合您的申请!

\n