如何解决iphone中的sqlite数据库
| 如何保留登录表中的所有行?我只能保留一行,为什么不能保留其他行?我使用错误的查询吗?请检查我的代码:#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 fileExistsAtPath:databasePath];
if(success)return;
Nsstring *databasePathFromApp=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@\"login.sqlite\"];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
[fileManager release];
}
-(void) Data
{
Gpass=@\"\";
Guname=@\"\";
sqlite3_stmt *detailStmt=nil;
//sqlite3 *databaseName;
NSArray *documentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES);
Nsstring *documentsDir =[documentPaths objectAtIndex:0];
Nsstring *databasePath=[documentsDir stringByAppendingPathComponent:@\"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:@\"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],sqlITE_TRANSIENT);
sqlite3_bind_text(detailStmt,2,[Gpassq UTF8String],sqlITE_TRANSIENT);
if (sqlITE_DONE!= sqlite3_step(detailStmt)) {
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);
}
解决方法
//Declare Class as Following to store user details UserDetails.h
@interface UserDetails : NSObject
{
NSString *strUserName;
NSString *strPassword;
}
@property (nonatomic,assign) NSString * strUserName;
@property (nonatomic,retain) NSString * strPassword;
//Declare Class as Following to store user details UserDetails.m
@implementation UserDetails
@synthesize strUserName,strPassword;
-(void)dealloc
{
[super dealloc];
[strUserName release];
[strPassword release];
}
//Declare Class as Following to store user details Your ApplicationDelegate.m file
-(NSMutableArray)getAllUserDetails
{
sqlite3_stmt *selectStmt = nil;
NSArray *documentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDir =[documentPaths objectAtIndex:0];
NSString *databasePath=[documentsDir stringByAppendingPathComponent:@\"login.sqlite\"];
[self chekAndCreateDatabase];
NSMutableArray *arrUsers = [[NSMutableArray alloc] init];
const char *sqlStatement = \"your query\";
if(sqlite3_prepare_v2(database,sqlStatement,-1,&selectStmt,NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while(sqlite3_step(selectStmt) == SQLITE_ROW)
{
UserDetails *objUserDetail = [[UserDetails alloc] init];
objUserDetail.userName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStmt,1)];
objUserDetail.password = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStmt,2)];
[arrUsers addObject:objUserDetail];
[objUserDetail release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(selectStmt);
selectStmt = nil;
}
在按钮单击事件中将此函数称为
-(IBAction)btnLogin
{
BOOL isUserExists = NO;
NSMutableArray *arrAllUsers = [loginAppDelegate getAllUserDetails];
//Normal Checking Stuff
for(UserDetails *objUser in arrAllUsers)
{
if([txtUserTextBox.text isEqualToString:objUser.strUserName] && [txtPasswordTextBox.text isEqualToString:objUser.strPassword])
{
//True Login
isUserExists = YES;
break;
}
}
//Check your stuff if user exists or not what to do
if(isUserExists)
{
Heading to next screen;
}
else
{
Alertmessage
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。