0

ビューコントローラーでユーザー名/パスワードログをプログラムしようとしていますが、アプリにエラーも警告もありませんが、データベースに既に存在するユーザー名を選択しても、常に「一致が見つかりません」と出力されます..I'これがコードです:

[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);
    }

}
4

1 に答える 1