2011-04-14 87 views
0

我正在創建一個應用程序,它首先通過檢查SQLite數據庫中的數據來檢查用戶是否有效。我在數據庫中創建了一個名爲loginchk的表,其中有三行兩列,分別爲unamepass從SQLite數據庫獲取比第一行多的問題

當我運行應用程序並在我的文本字段中輸入用戶名和密碼值時,它會檢查用戶名和密碼是否在數據庫中。如果不是,則會顯示一個警告視圖,顯示「請輸入正確的用戶名」。

在我的代碼中存在一個問題:它只讀取第一行,即當我輸入第一行成功的用戶名和密碼值,並且我輸入的所有其他值都不起作用。

可能是什麼問題?

謝謝。

#import "loginAppDelegate.h" 
#import "global.h" 
#import <sqlite3.h> 
#import "logincontroller.h" 
@implementation loginAppDelegate 
@synthesize window; 
@synthesize loginView; 
//[email protected]"login.sqlite"; 
-(void) chekAndCreateDatabase 
{ 
BOOL success; 
//sqlite3 *[email protected]"login.sqlite"; 
NSFileManager *fileManager=[NSFileManager defaultManager]; 
NSArray *documentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir =[documentPaths objectAtIndex:0]; 
NSString *databasePath=[documentsDir stringByAppendingPathComponent"login.sqlite"]; success=[fileManager fileExistsAtPathatabasePath]; 
if(success)return;  
NSString *databasePathFromApp=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent"login.sqlite"]; 
     [fileManager copyItemAtPathatabasePathFromApp toPathatabasePath error:nil]; [fileManager release]; 
} 

-(void) Data 
{  
[email protected]""; 
[email protected]""; 
sqlite3_stmt *detailStmt=nil;  
//sqlite3 *databaseName;  
NSArray *documentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir =[documentPaths objectAtIndex:0]; 
NSString *databasePath=[documentsDir stringByAppendingPathComponent{angry_smile}"login.sqlite"]; 
[self chekAndCreateDatabase]; 
     sqlite3 *database; 
if (sqlite3_open([databasePath UTF8String],&database)==SQLITE_OK) 
{  
    if (detailStmt==nil) 
    {  
     const char *sql= "select *from Loginchk"; //where uname='%?'and password='%?'";  //NSString *sql = [[NSString alloc] initWithFormat{angry_smile}"SELECT * FROM Loginchk WHERE uname ='%@' and password ='%@' ",Uname.text,Password.text];  
if (sqlite3_prepare_v2(database,sql,-1,&detailStmt,NULL)==SQLITE_OK) 
{         
     sqlite3_bind_text(detailStmt,1,[Gunameq UTF8String],-1,SQLITE_TRANSIENT); 
        sqlite3_bind_text(detailStmt,2,[Gpassq UTF8String],-1,SQLITE_TRANSIENT);     
if (SQLITE_DONE!= sqlite3_step(detailStmt)) 


{      
    Guname=[NSString stringWithUTF8Stringchar*)sqlite3_column_text(detailStmt,0)];   

Gpass =[NSString stringWithUTF8Stringchar*)sqlite3_column_text(detailStmt,1)];      

NSLog(@"'%@'",Guname);   
    NSLog(@"'%@'",Gpass);   }  
    }  sqlite3_finalize(detailStmt);  
} 
} sqlite3_close(database); 
    } 

這是我的登錄控制器;在這裏,我打電話給我的應用程序委託的功能data

-(IBAction)buttonPressed:(id)sender 
{  
Gpassq=Password.text; 
    Gunameq=Uname.text; 
     NSLog(@"%@%@",Gunameq,Gpassq); 
    loginAppDelegate *appDelegate =(loginAppDelegate *)[[UIApplication sharedApplication]delegate]; 
[appDelegate Data]; 
    if ([Uname.text isEqualToString:Guname]&&[Password.text isEqualToString:Gpass]) 
{   
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Success" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    }  
    else 
{ 
    UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"PleaseEnterCorrectUserName and password" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[alert show];  
[alert release]; 
    } 
    } 

回答

0

變化

if (SQLITE_DONE!= sqlite3_step(detailStmt)) 

while (SQLITE_DONE!= sqlite3_step(detailStmt)) 

更新1

使用此代碼 -

-(void) Data 
{  
    [email protected]""; 
    [email protected]""; 
    sqlite3_stmt *detailStmt=nil;  
    //sqlite3 *databaseName;  
    NSArray *documentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir =[documentPaths objectAtIndex:0]; 
    NSString *databasePath=[documentsDir stringByAppendingPathComponent:@"login.sqlite"]; 
    [self chekAndCreateDatabase]; 


    sqlite3 *database; 
    if (sqlite3_open([databasePath UTF8String],&database)==SQLITE_OK) 
    {  

     const char *sql= "select *from Loginchk"; //where uname='%?'and password='%?'";  //NSString *sql = [[NSString alloc] initWithFormat{angry_smile}"SELECT * FROM Loginchk WHERE uname ='%@' and password ='%@' ",Uname.text,Password.text];  
     sqlite3_stmt *detailStmt; 
     if (sqlite3_prepare_v2(database,sql,-1,&detailStmt,NULL)==SQLITE_OK) 
     {         
      while(sqlite3_step(detailStmt) == SQLITE_ROW) 
      { 

       Guname=[NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt,0)];   
       Gpass =[NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt,1)];      

       NSLog(@"'%@'",Guname);   
       NSLog(@"'%@'",Gpass);   

      }  

     } 

     sqlite3_finalize(detailStmt);  
     sqlite3_close(database); 

    } 


} 
+0

如果去除,並添加,而不是檢查我的病情直接進入else部分 – Rani 2011-04-14 11:01:20

+0

你確定你在你的數據庫中的多行..打印數據庫的路徑和開放它使用一些SQLite客戶端,並檢查表.. – Saurabh 2011-04-14 11:11:12

+0

我不能相信這是行不通的..我測試了這個代碼!你一定有其他問題! – Saurabh 2011-04-14 11:46:35

0

試試這個

\-(void) Data 
{  
[email protected]""; 
[email protected]""; 
sqlite3_stmt *detailStmt=nil;  
//sqlite3 *databaseName;  
NSArray *documentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir =[documentPaths objectAtIndex:0]; 
NSString *databasePath=[documentsDir stringByAppendingPathComponent{angry_smile}"login.sqlite"]; 
[self chekAndCreateDatabase]; 
     sqlite3 *database; 
if (sqlite3_open([databasePath UTF8String],&database)==SQLITE_OK) 
{  
    if (detailStmt==nil) 
    {  
     const char *sql= "select *from Loginchk"; //where uname='%?'and password='%?'";  //NSString *sql = [[NSString alloc] initWithFormat{angry_smile}"SELECT * FROM Loginchk WHERE uname ='%@' and password ='%@' ",Uname.text,Password.text];  
if (sqlite3_prepare_v2(database,sql,-1,&detailStmt,NULL)==SQLITE_OK) 
{         
     sqlite3_bind_text(detailStmt,1,[Gunameq UTF8String],-1,SQLITE_TRANSIENT); 
        sqlite3_bind_text(detailStmt,2,[Gpassq UTF8String],-1,SQLITE_TRANSIENT);     
// Loop through the results 
      while(sqlite3_step(detailStmt) == SQLITE_ROW) { 


{      
    Guname=[NSString stringWithUTF8Stringchar*)sqlite3_column_text(detailStmt,0)];   

Gpass =[NSString stringWithUTF8Stringchar*)sqlite3_column_text(detailStmt,1)];      

NSLog(@"'%@'",Guname);   
    NSLog(@"'%@'",Gpass);   }  
    }  sqlite3_finalize(detailStmt);  
} 
} sqlite3_close(database); 
    }