将SQLite3连接到app

时间:2011-12-15 13:15:52

标签: iphone sqlite

HI所有我想尝试将SQLite连接到我的应用程序,但它不工作显示总是登录失败! 我认为因为这条线而失败了

//从loginTb中选择b,c,其中b ='%@'和c ='%@'“,loginName.text,password.text];

请找到我的整个代码供您参考。

SQLite代码:

Last login: Thu Dec 15 11:24:55 on console
Venkateshs-Mac-mini:~ venkateshnarasimhan$ sqlite3 login.db
SQLite version 3.7.5
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE loginTb (a integer, b string, c string);
INSERT INTO "loginTb" VALUES(1,'pradeep','password');
INSERT INTO "loginTb" VALUES(2,'kripya','password');
INSERT INTO "loginTb" VALUES(3,'ravi','password');
INSERT INTO "loginTb" VALUES(4,'venkatesh','password');
INSERT INTO "loginTb" VALUES(5,'veeru','password');
COMMIT;
sqlite> 

XCODE:

    -(BOOL)checkindatabase
    {
        NSArray *dirPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir =[dirPath objectAtIndex:0];
        databasePath =[[NSString alloc] initWithString:[docDir stringByAppendingPathComponent:@"login.db"]];

        if(sqlite3_open([databasePath UTF8String], &contactDB) == SQLITE_OK)
        {
            NSLog(@"open");
            NSString *sql = [[NSString alloc] initWithFormat:@"select b,c from loginTb where b='%@' and c='%@'",loginName.text,password.text];        
            [sql UTF8String];
            sqlite3_stmt *statement;
            if (sqlite3_prepare_v2(contactDB, [sql UTF8String], -1, &statement, NULL) == SQLITE_OK)
            {   if(sqlite3_step(statement) == SQLITE_ROW)
            { 
                //user name is correct
                //if u want to print in log use below code  
                uid =[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
                loginName.text = uid;

               pwd =[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
                password.text = pwd;
                return YES;

            }else{ uid=0;pwd=0;}
            }            
            sqlite3_finalize(statement);
        }
        sqlite3_close(contactDB);
        return NO;
    }

-(IBAction)homePage: (id)sender
{
    if( [self checkindatabase] )
    {
        homepage *hvc = [[homepage alloc]initWithNibName: nil bundle: nil];
        hvc.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:hvc animated: YES];
    }
    else
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Login Failed!!!"
                                                        message:@"Check your Id and Password" delegate:nil 
                                              cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    } 
} 

2 个答案:

答案 0 :(得分:0)

 if(sqlite3_open([databasePath UTF8String], &contactDB) == SQLITE_OK)
    {
        NSLog(@"open");
        NSString *sql = [[NSString alloc] initWithFormat:@"select b,c from loginTb where b=\"%@\" and c=\"%@\"",loginName.text,password.text];     // using this line \"%@\" in b=\"%@\"   
        [sql UTF8String];
        sqlite3_stmt *statement;
  your code follows........

答案 1 :(得分:0)

我建议您不要尝试调试SQLite,而是尝试使用像FMDB这样的包装器,它非常易于设置且易于理解。