gpt4 book ai didi

iphone - SQLite for iOS 中的多个查询

转载 作者:行者123 更新时间:2023-12-01 18:32:45 24 4
gpt4 key购买 nike

在这里需要帮助。

我设法让drinkName 和类别工作。 http://dl.dropbox.com/u/418769/2.png

但我需要区分类别并获取 SQL 数据库的计数值。

像这样的http://dl.dropbox.com/u/418769/2.png然后这个 http://dl.dropbox.com/u/418769/3.png

我需要怎么做才能完成?

我需要从饮料中运行 SELECT drinkID,drinkName,

从饮料中选择 DISTINCT 类别和

计算每个类别的行..可以做到吗?

这是我的数据库看起来像..http://dl.dropbox.com/u/418769/4.png

我正在关注 http://mybankofknowledge.wordpress.com/2010/12/01/accessing-sqlite-from-iphone-table-view/

好的,它有点工作.....我解决了这个错误

* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[NSMutableArray objectAtIndex:]:索引 5 超出范围 [0 .. 4]'

  • (void) getInitialDataToDisplay:(NSString *)dbPath {
    DrinkTabsAndNavAppDelegate *appDelegate = (DrinkTabsAndNavAppDelegate *)[[UIApplication sharedApplication] delegate];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
    const char *sql = "SELECT drinkID, drinkName FROM drinks";
    //const char *sql = "SELECT drinkName, categories FROM drinks";
    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);
    Drink *drinkObj = [[Drink alloc] initWithPrimaryKey:primaryKey];
    drinkObj.drinkName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)];
    //drinkObj.categories = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];

    drinkObj.isDirty = NO;
    [appDelegate.drinksArray addObject:drinkObj];
    [drinkObj release];
    }


    }

    } 否则 sqlite3_close(数据库);//关闭db释放所有内存
    }
  • (void) getCategory:(NSString *)dbPath {
    DrinkTabsAndNavAppDelegate *appDelegate = (DrinkTabsAndNavAppDelegate *)[[UIApplication sharedApplication] delegate];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
    const char *sql = "SELECT DISTINCT category FROM drinks";
    //const char *sql = "SELECT drinkName, categories FROM drinks";
    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);
    Drink *catObj = [[Drink alloc] initWithPrimaryKey:primaryKey];
    catObj.category = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
    //drinkObj.categories = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];
    NSLog(@"run here");
    catObj.isDirty = NO;
    [appDelegate.categoryArray addObject:catObj];
    [catObj release];
    }


    }

    } 否则 sqlite3_close(数据库);//关闭db释放所有内存
    }
  • 最佳答案

    如果您想计算每个类别中的饮料数量:

               select category, count(drinkid) as DrinksInThisCategory
    from drinks
    group by category

    但是一个正确规范化的数据库将有一个单独的 CATEGORIES 表,并且您的 DRINKS 表中有一个 categoryid。

    关于iphone - SQLite for iOS 中的多个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6859377/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com