比较 NSString 和 SQLite 数据库字段

发布于 2024-12-16 01:34:31 字数 1145 浏览 0 评论 0原文

我有这些代码从 SQLite 数据库检索值并将其与头文件中声明的 NSString 进行比较。

从数据库检索:

NSString *sql = [[NSString alloc] initWithFormat:@"SELECT TESTONE, TESTTWO, TESTTHREE, TESTFOUR FROM STUDENTS WHERE NAME='%@'",Name];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)== SQLITE_OK)
 {
    while(sqlite3_step(statement) == SQLITE_ROW){
        char *one = (char *) sqlite3_column_text(statement,0);
        tOne = [[NSString alloc] initWithUTF8String:one];
        char *two = (char *) sqlite3_column_text(statement,1);
        tTwo = [[NSString alloc] initWithUTF8String:two];
        char *three = (char *) sqlite3_column_text(statement,2);
        tThree = [[NSString alloc] initWithUTF8String:three];
        char *four = (char *) sqlite3_column_text(statement,3);
        tFour = [[NSString alloc] initWithUTF8String:four];
    }
 }  

比较字符串:

if(tOne == @"Yes")
{
    //blablabla
}
else if(tOne == @"No")
{
    //blablabla
}

它似乎根本没有进入“IF”。
我已经仔细检查了数据库中的字段及其 TEXT 数据类型。
我制作了一个 UILabel 来显示 tOne 值,它显示“是”。

我可以知道比较过程中出了什么问题或哪里出了问题吗?

I have these codes that retrieve value from the SQLite database and comparing it to a NSString declared in the header file.

Retrieving from database:

NSString *sql = [[NSString alloc] initWithFormat:@"SELECT TESTONE, TESTTWO, TESTTHREE, TESTFOUR FROM STUDENTS WHERE NAME='%@'",Name];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)== SQLITE_OK)
 {
    while(sqlite3_step(statement) == SQLITE_ROW){
        char *one = (char *) sqlite3_column_text(statement,0);
        tOne = [[NSString alloc] initWithUTF8String:one];
        char *two = (char *) sqlite3_column_text(statement,1);
        tTwo = [[NSString alloc] initWithUTF8String:two];
        char *three = (char *) sqlite3_column_text(statement,2);
        tThree = [[NSString alloc] initWithUTF8String:three];
        char *four = (char *) sqlite3_column_text(statement,3);
        tFour = [[NSString alloc] initWithUTF8String:four];
    }
 }  

Comparing string:

if(tOne == @"Yes")
{
    //blablabla
}
else if(tOne == @"No")
{
    //blablabla
}

It doesn't seem to go into the 'IF' at all.
I have double-checked the field in the database and its TEXT datatype.
I made a UILabel to display the tOne value and it shows "Yes".

May i know what or where went wrong during the comparison?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青巷忧颜 2024-12-23 01:34:31

你不能像这样比较字符串,你必须使用 isEqualToString:

if([tOne isEqualToString:@"Yes"])
{
    //blablabla
}
else if([tOne isEqualToString:@"No"])
{
    //blablabla
}

You can't compare strings like this, you have to use isEqualToString:

if([tOne isEqualToString:@"Yes"])
{
    //blablabla
}
else if([tOne isEqualToString:@"No"])
{
    //blablabla
}
悟红尘 2024-12-23 01:34:31

您当前正在比较 NSString 对象的地址,而不是其内容。使用 compare:isEqualToString: 系列方法来比较字符串的内容

参考

You are currently comparing the address of the NSString object, not its contents. Use the compare: or isEqualToString: family of methods to compare the content of the string.

Reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文