我正在创建一个应用程序,它首先通过检查SQLite数据库中的数据来检查用户是否有效。我在数据库中创建了一个名为loginchk
的表,其中包含三行和两列uname
和pass
。
当我运行应用程序并在我的文本字段中输入用户名和密码值时,它会检查用户名和密码是否在数据库中。如果不是,则会显示一个警告视图,上面写着“请输入正确的用户名”。
在我的代码中存在一个问题:它只读取第一行,即当我输入第一行中的用户名和密码值时,它输入的所有其他值都不起作用。< / p>
可能是什么问题?
感谢。
#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);
}
这是我的登录控制器;我在这里调用我的app delegate的函数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 :(得分:0)
更改
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);
}
}
答案 1 :(得分:0)
试试这个
\-(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);
}