从数据库检索数据时遇到问题
我正在尝试使用 sqlite 数据库验证用户输入的用户名和密码。如果用户名和密码存在于数据库中,则用户不需要登录。但如果他的用户名和密码不匹配,他需要登录。我首先在我的应用程序委托中添加了以下代码:
- (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 toPath: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);
}
最初我检查了数据库是否存在。如果不存在,我创建了一个。然后我从数据库中读取。
然后在我的登录控制器的登录操作中,我添加了以下代码:
-(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];
}
}
此代码检查我的用户名和密码是否等于数据库中指定的字符串,如果相等,则应该从数据库中读取。否则它应该注册。但是当我运行我的应用程序时我的应用程序加载,当我输入用户名和密码并单击登录按钮时,应用程序崩溃。 我收到有关登录操作的警告,JourneyAppDelegate 可能不会响应 -readLoginFromDatabase
可能是什么问题,
谢谢
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 toPath: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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,如果崩溃了,你也需要在这里发布崩溃日志。
第二件事 - 不要使用 sqllite 来存储用户名/密码。一定要使用钥匙扣。
第三件事 - 如果你确实想使用 sqllite 来存储某些东西,为什么不通过 CoreData 呢?
关于您的警告 - 似乎您也忘记将
readLoginFromDatabase
方法添加到头文件中。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.如果您想删除警告,请将此代码添加到您的 JourneyAppDelegate.h
If you want to remove the warning add this code to your JourneyAppDelegate.h