getting a problem while retrieving data from database
i am trying to validate username and password entered by the user with the sqlite database.if the username and password are present in the database the user need not to log in.But if his username and password does not match he need to login. i have added the following code in my appdelegate first:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
databaseName =@"journeymapper.db3";
NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentsPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
[self checkAndCreateDatabase];
}
-(void)checkAndCreateDatabase{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if (success)
return;
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp to开发者_JAVA技巧Path:databasePath error:nil];
[fileManager release];
}
-(void)readLoginFromDatabase{
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK){
const char *sqlStatement = "Select Username,Password from UserInformation";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK){
NSString *aUserName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aPassword = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
Initially i have checked if database is present .If not i have created one.Then i have read from database.
Then in my login controller's login action i have added the following code:
-(IBAction)login:(id)sender{
journey = (JourneyAppDelegate *)[[UIApplication sharedApplication]delegate];
if ([mUserName.text isEqualToString:@"shardulprabhu"] && [mPassword.text isEqualToString:@"password"]) {
[journey readLoginFromDatabase];
NSLog(@"journey read");
}
else{
[self signUp];
}
}
this code checks if myusername and password is equaltostring specified the one in database and if it is equal it should read from database.else it should signup.But when i run my app my app loads and when i enter username and password and click login button the app crashes. i am having a warning on login action that JourneyAppDelegate may not respond to-readLoginFromDatabase
What may be the problem
thanks
At first, if it crashes, you need to post crash log here too.
Second thing - do not use sqllite for username / password storage. Do use keychain.
Third thing - if you do want to use sqllite to store something, why not via CoreData?
About your warning - it seems that you forgot to add your readLoginFromDatabase
method to header file too.
If you want to remove the warning add this code to your JourneyAppDelegate.h
-(void)readLoginFromDatabase;
精彩评论