0
HI所有我想連接SQLite到我的應用程序,但它不工作顯示總是登錄失敗! 我想是因爲該行將SQLite3連接到應用程序
//選擇b,由loginTb的C失敗其中b = '%@' 和c = '%@」',loginName.text,password.text];
請找我的整個代碼爲您refernce
的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];
}
}