选择查询不起作用

选择查询不起作用

- (UserInfo*)getCurrentUserInfo:(NSString*)userName
{
    UserInfo *userInfo = [[UserInfo alloc]init];

    sqlite3 *database;
    sqlite3_stmt *selectstmt;
    NSLog(@"userName:%@",userName);
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        NSString *sqlString = [NSString stringWithFormat:@"SELECT USER_LEVEL FROM USER_INFO WHERE USER_NAME = '%@'" , userName];
        NSLog(@"getCurrentUserInfo:%@",sqlString);
        const char *SqlCommand = [sqlString UTF8String];
        if (sqlite3_prepare_v2(database, SqlCommand, -1, &selectstmt, NULL) == SQLITE_OK) {
            NSLog(@"success!");
            while (sqlite3_step(selectstmt) == SQLITE_ROW) {
                NSString *userInfoStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
                NSLog(@"select result:%@",userInfoStr);
            }
        }
        sqlite3_finalize(selectstmt);
    }
    sqlite3_close (database);
    return userInfo;
}


以下是我的日志输出:

2012-03-06 17:50:11.556 MagicWords[508:f803] userName:Tan
2012-03-06 17:50:11.557 MagicWords[508:f803] getCurrentUserInfo:SELECT USER_LEVEL FROM USER_INFO WHERE USER_NAME = 'Tan'


它不会打印“成功”,所以sqlite3_prepare_v2不会返回yes。但是我的数据库可以:

我找不到问题吗?

最佳答案

添加sqlite错误消息。它将深入了解内部的情况。如果有关数据库的所有信息都正确,请检查表名称。

UserInfo *userInfo = [[UserInfo alloc]init];

    sqlite3 *database;
    sqlite3_stmt *selectstmt;
    NSLog(@"userName:%@",userName);
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        NSString *sqlString = [NSString stringWithFormat:@"SELECT USER_LEVEL FROM USER_INFO WHERE USER_NAME = '%@'" , userName];
        NSLog(@"getCurrentUserInfo:%@",sqlString);
        const char *SqlCommand = [sqlString UTF8String];
        if (sqlite3_prepare_v2(database, SqlCommand, -1, &selectstmt, NULL) == SQLITE_OK) {
            NSLog(@"success!");
            while (sqlite3_step(selectstmt) == SQLITE_ROW) {
                NSString *userInfoStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
                NSLog(@"select result:%@",userInfoStr);
            }
        }
      //Add the error message here.
       else
        {

         NSLog(@"%s",sqlite3_errmsg(database));

        }
       ///////
        sqlite3_finalize(selectstmt);
    }
    sqlite3_close (database);
    return userInfo;

关于iphone - sqlite,选择查询不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9581464/

10-10 08:20