Thursday, 22 August 2013

Can any one please tell me why my code isn't working?

Can any one please tell me why my code isn't working?

I am trying to program a username/password log in view controller, there
is no errors, no warnings in my app but it always output out "match not
found" even when i select a username that already exist in my
database..I'd really appreciate it if u could help this is the code:
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
docsDir = dirPaths [0];
_databasePath = [[NSString alloc]initWithString:[docsDir
stringByAppendingPathComponent:@"bank.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath:_databasePath] == NO)
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_bankDb) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = " CREATE TABLE IF NOT EXISTS USER (USERNAME
TEXT PRIMARY KEY, PASSWORD TEXT)";
if (sqlite3_exec(_bankDb, sql_stmt, NULL, NULL, &errMsg) !=
SQLITE_OK)
{
_status.text= @"failed to create table";
}
sqlite3_close(_bankDb);
} else {
_status.text=@"failed to open/create database";
}
}
- (IBAction)findContact:(id)sender {
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_bankDb) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"SELECT USERNAME,
PASSWORD FROM USER WHERE USERNAME=\"%@\"", _usernameTextField.text];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_bankDb, query_stmt, -1, &statement, NULL) ==
SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *usernameField=[[NSString
alloc]initWithUTF8String:(const char *)
sqlite3_column_text(statement, 0)];
_usernameTextField.text=usernameField;
NSString *passwordField = [[NSString alloc]
initWithUTF8String:(const char *)
sqlite3_column_text(statement, 1) ];
_passwordTextField.text=passwordField;
_status.text=@"match found";
} else {
_status.text=@"match not found";
}
sqlite3_finalize(statement);
}
sqlite3_close(_bankDb);
}
}

No comments:

Post a Comment