比较 NSString 和 SQLite 数据库字段
我有这些代码从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能像这样比较字符串,你必须使用
isEqualToString:
You can't compare strings like this, you have to use
isEqualToString:
您当前正在比较
NSString
对象的地址,而不是其内容。使用compare:
或isEqualToString:
系列方法来比较字符串的内容。参考。
You are currently comparing the address of the
NSString
object, not its contents. Use thecompare:
orisEqualToString:
family of methods to compare the content of the string.Reference.