草庐IT

objective-c - SQLite 数据显示

coder 2023-07-20 原文

我是 SQLite 的新手。我有一个包含不同日期的 UITableview。 (周一至周日)。例如,当我在星期一单击时,另一个 viewcontroller 包含其中也包含一个 UITableview。在同一个 View Controller 中,我有一个 UIButton,当我点击它时,我可以将数据添加到我的 SQLite 数据库 [A],我插入 name 和星期几(星期几在这个例子中是“星期一”,那是因为我点击了星期一 View Controller )。

当我插入一个名称 时,它会出现在我的表格 View 中。但是当我回到我的第一个 View Controller 时,我在星期三点击例如我添加的数据也出现在那里。

所以我的问题是;我怎样才能显示我在星期一插入的名字,只在星期一的表格 View 中而不是其他日子(表格 View )

更多信息:

因此,当用户在“星期一”中添加名称时,我会将添加名称的星期几发送到 SQLite 数据库,当用户在星期三添加名称时,我会在星期三发送“星期三”等。

数据库咖啡看起来像=

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 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);
}

插入:

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

此插入:星期一星期二星期三星期四星期五星期六或星期日

但是我如何在星期一的 TableView 中显示星期一插入的数据以及在星期二 Controller 等中星期二插入的数据。

我试过了;

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)

/* 开始 */ 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;
}

调用函数 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]];

最佳答案

很难理解您的问题,但我认为您最好打开一个新项目并从 Core Data 开始。它易于理解,并且比 SQLite 更快。

Core Data 是 Apple 为开发人员提供的框架,被描述为“架构驱动的对象图管理和持久性框架”。这到底是什么意思?该框架管理数据的存储位置、存储方式、数据缓存和内存管理。它是通过 3.0 iPhone SDK 版本从 Mac OS X 移植到 iPhone 上的。

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

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

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

关于objective-c - SQLite 数据显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9445099/

有关objective-c - SQLite 数据显示的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  5. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  6. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  7. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  8. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  9. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  10. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

随机推荐