从 SQLite DB 获取多于第一行的问题

发布于 2024-11-01 23:41:49 字数 3683 浏览 0 评论 0原文

我正在创建一个应用程序,它首先通过检查 SQLite 数据库中的数据来检查用户是否有效。我在数据库中创建了一个名为 loginchk 的表,其中包含三行两列:unamepass

当我运行应用程序并在文本字段中输入用户名和密码值时,它会检查用户名和密码是否在数据库中。如果不是,它将显示一个警报视图,显示“请输入正确的用户名”。

在我的代码中存在一个问题:它只读取第一行,即当我输入第一行中的用户名和密码值时,它会成功,而我输入的所有其他值都不起作用。

可能是什么问题?

谢谢。

#import "loginAppDelegate.h"
#import "global.h" 
#import <sqlite3.h>
#import "logincontroller.h" 
@implementation loginAppDelegate 
@synthesize window; 
@synthesize loginView; 
//databaseName=@"login.sqlite";
 -(void) chekAndCreateDatabase 
{   
BOOL success;   
//sqlite3  *databaseName=@"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 
{     
 Gpass=@"";   
Guname=@""; 
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];  
  } 
   } 

I am creating an application which first checks whether the user is valid or not by checking data from an SQLite database. I have created a table named loginchk in the database with three rows and two columns in it, uname and pass.

When I run the application and enter the username and password values in my text fields, it checks if the username and password are in the database. If they are not, it will present an alert view that says "Please enter correct username".

In my code there is a problem: it reads only the first row, i.e., when I enter the username and password values that are in the first row it succeeds, and all the other values that I enter do not work.

What could be the problem?

Thanks.

#import "loginAppDelegate.h"
#import "global.h" 
#import <sqlite3.h>
#import "logincontroller.h" 
@implementation loginAppDelegate 
@synthesize window; 
@synthesize loginView; 
//databaseName=@"login.sqlite";
 -(void) chekAndCreateDatabase 
{   
BOOL success;   
//sqlite3  *databaseName=@"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 
{     
 Gpass=@"";   
Guname=@""; 
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); 
  }   

This my login controller; here I am calling my app delegate's function 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];  
  } 
   } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄人书 2024-11-08 23:41:49

进行更改

if (SQLITE_DONE!= sqlite3_step(detailStmt)) 

使用

while  (SQLITE_DONE!= sqlite3_step(detailStmt))

更新 1

,使用此代码 -

-(void) Data 
{     
    Gpass=@"";   
    Guname=@""; 
    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); 

    }   


}

change

if (SQLITE_DONE!= sqlite3_step(detailStmt)) 

with

while  (SQLITE_DONE!= sqlite3_step(detailStmt))

Update 1

use this code -

-(void) Data 
{     
    Gpass=@"";   
    Guname=@""; 
    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); 

    }   


}
源来凯始玺欢你 2024-11-08 23:41:49

试试这个

\-(void) Data 
{     
 Gpass=@"";   
Guname=@""; 
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); 
  }

Try this

\-(void) Data 
{     
 Gpass=@"";   
Guname=@""; 
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); 
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文