开发者

login page problem in iphone

I'm getting the same problem, one of stackoverflow which is:

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
    //const char *sql ="select Username@'%@',Password@'%@' from userinformation";
    NSString *sql = [[NSString alloc] initWithFormat:@"select * from UserInformation where UserName='%@' and Password='%@' ",Username.text,Password.text];
    sqlite3_stm开发者_JAVA技巧t *selectSatement;

    // Here i am getting the problem.Im not sure why sqlite3_prepare_v2 ins't meeting SQLITE_OK 
    if( sqlite3_prepare_v2(database, [sql UTF8String], -1, &selectSatement, NULL) == SQLITE_OK)
    {
        //-------------------------------------
        //Loop all the 
        NSString *user1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectSatement, 0)];
        NSLog(@"%@",user1);
    }
}

Any help with this is greatly appreciated.


First of all: if you open the database, you should also close it by using sqlite3_close(database)

I wrote in the code below an assert-statement, which will tell you, if something is wronmg with your query. Further on you might get an error, if your password or username is for some reason "nil", that's why I adapted the way you set the username and password a bit. If the value does not exist, its "0"

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
    char *errorMsg;

    NSString *sql = [[NSString alloc] initWithFormat: @"SELECT * FROM UserInformation WHERE UserName=\"%@\" and Password =\"%@\"", Username.text, Password.text];
    if (sqlite3_exec (database, [sql UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK)
    {
        sqlite3_close(database);
        NSAssert1(0, @"Error selecting data: %s", sqlite3_errmsg(database));
    }
    else
    {
        NSString *user1 = [NSString stringWithUTF8String:((char *)sqlite3_column_text(compiledStatement, 0) ? (char *)sqlite3_column_text(compiledStatement, 0) : (char *)"0")];
        NSString *password1 = [NSString stringWithUTF8String:((char *)sqlite3_column_text(compiledStatement, 1) ? (char *)sqlite3_column_text(compiledStatement, 1) : (char *)"0")];
        NSLog(@"%@: %@",user1,password1);
    }
    [sql release];
}
sqlite3_close(database);

What you should also keep in mind is, that the database should exist at the intended path, by calling for example the following method

-(void) checkAndCreateDatabase
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:dbPath]) return;
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
    [fileManager release];
}


Check the query itself, print it in log and check it by copying it from log and executing directly in the database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜