Tan
Tan

Reputation: 179

sqlite, select query doesn't work

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

the following is my log output:

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'

It doesn't print "success",so sqlite3_prepare_v2 don't return yes.but my database is ok:

I can't find the problem?

Upvotes: 1

Views: 3365

Answers (3)

hchouhan02
hchouhan02

Reputation: 946

try this when you are converting sqlString to char

const char *SqlCommand = (char *)[sqlString cStringUsingEncoding:NSUTF8StringEncoding];

Upvotes: 0

Vignesh
Vignesh

Reputation: 10251

Add sqlite error message . It will give an insight of what is going on inside. If all about your database is correct ,check the table name.

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;

Upvotes: 2

Anil Kothari
Anil Kothari

Reputation: 7733

Remove the quotes inside ' ' and Use Semicolon at the end of the sqlString.

NSString *sqlString = [NSString stringWithFormat:@"SELECT USER_LEVEL FROM USER_INFO WHERE USER_NAME = %@;" , userName];

Upvotes: 0

Related Questions