从 SQLite 数据库获取 BOOL 的问题 [英] Problem getting a BOOL from a SQLite DB

查看:47
本文介绍了从 SQLite 数据库获取 BOOL 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用 SQLite 数据库来存储信息的应用程序.我的数据库表包括 5 个字段:gameID(主键,int)、gameName(Varchar)、isLocked、isHidden 和 isFavorite(所有布尔值).我已经在 FireFox 中使用 SQLite Manager 设置了数据库.

I'm writing an app using a SQLite db to store information. My db table includes 5 fields: gameID (the primary key, int), gameName (Varchar), isLocked, isHidden, and isFavorite (all bools). I've set up the db using SQLite Manager in FireFox.

+ (void) getInitialDataToDisplay:(NSString *)dbPath {

FightingGamesGuideAppDelegate *appDelegate = (FightingGamesGuideAppDelegate *)[[UIApplication sharedApplication] delegate];

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {


        //My second thought is that this line is incorrect.  (see below)
    const char *sql = "select gameID, gameName, isLocked, isHidden, isFavorite from GameTable";
    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);
            Games *coffeeObj = [[Games alloc] initWithPrimaryKey:primaryKey];
            //getGameName
            coffeeObj.gameName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];

            //I think these three lines are where the problem is
            coffeeObj.isLocked=(BOOL)sqlite3_column_int(selectstmt, 2);
            coffeeObj.isHidden=(BOOL)sqlite3_column_int(selectstmt, 3);
            coffeeObj.isFavorite=(BOOL)sqlite3_column_int(selectstmt, 4);

              //This line is only to check if the data loaded properly
            NSLog(@"%i, %i, %i",isLocked, isHidden, isFavorite);


            [appDelegate.coffeeArray addObject:coffeeObj];

            [coffeeObj release];
        }
    }
}
else{
    sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}

}

我的问题:这是如何从 SQLite 数据库获取布尔值?有没有更好的方法,例如使用 int 作为 bool 代替?如果我的代码都是正确的,你对我如何做到这一点还有其他建议吗?

My question: Is this how to get a bool from a SQLite db? Is there a better way, such as using an int as a bool instead? If my code is all correct, do you have any other suggestion for how I can do this?

我知道除了那三行(以及下一行)之外的代码都可以正常工作.在数据库中,我将三个布尔值随机分配为 YES 或 NO.我也试过 1 和 0,没有效果.

I know that the code aside from those three lines (and the following line) work properly. In the db, I've randomly assigned the three bools to YES or NO. I've also tried 1 and 0, to no effect.

如果这些行是正确的,我的第二个想法是第 9 行是错误的.我把它从const ... ="select gameID, gameName from GameTable"修改过来,gameID 和 gameName 仍然可以正常工作.

If the lines are correct, my second thought is that line 9 is wrong. I modified it from "const ... ="select gameID, gameName from GameTable", and gameID and gameName still work properly.

我的第三个想法是我没有正确更新数据库.除了询问是否有特定的保存"按钮/列表项之外,我不会深入讨论.如果我更新了一个游戏名称,它会显示在我的应用中,建议数据库自动保存.

My third thought is that I'm not updating the db correctly. I won't go into that, besides to ask if there's a specific "save" button/list item. If I update a gameName, that shows in my app, suggesting the db saves automatically.

在查看其他问题时,我只发现 CoreData 是一个更好的主意的建议.但是,如果我在正确的轨道上,我只需要这三行就可以了,我就完成了(这部分).

In looking through the other questions, I've only found suggestions that CoreData is a better idea. However, if I'm on the right track, I only need these three lines to work, and I'm done (with this part).

另外-我知道除了将它们打印到控制台之外,没有对 bool 变量进行任何处理.这是我的下一个挑战.

Aside-I know that nothing is done with the bool variables besides printing them to the console. That's my next challenge.

感谢您的宝贵时间!

推荐答案

除了 p.campbell 的回答 (+1),如果您存储的是 YES/,您还可以像这样回忆您的 BOOL 值您的数据库中没有:

In addition to p.campbell's answer (+1), you could recall your BOOL value like this if you are storing YES/NO in your DB:

  coffeeObj.isLocked=[[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)] isEqualToString:@"YES"];
  coffeeObj.isHidden=[[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)] isEqualToString:@"YES"];
  coffeeObj.isFavorite=[[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)] isEqualToString:@"YES"];

这篇关于从 SQLite 数据库获取 BOOL 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆