0

我正在尝试在视图控制器中编写用户名/密码日志,我的应用程序中没有错误,没有警告,但即使我选择数据库中已经存在的用户名,它也总是输出“找不到匹配”。如果你能帮忙,我真的很感激这是代码:

[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

4

You should do the following things,

  1. look at sqlite3_errmsg values if any queries fail (e.g. sqlite3_prepare_v2 does not return SQLITE_OK or sqlite3_step does not return either SQLITE3_DONE or SQLITE3_ROW);

  2. Do not use stringWithFormat with your queries, but rather use ? placeholders and bind values with sqlite3_bind_text

Thus:

if (sqlite3_open(dbpath, &_bankDb) == SQLITE_OK)
{
    const char *query_stmt = "SELECT USERNAME, PASSWORD FROM USER WHERE USERNAME=?";
    if (sqlite3_prepare_v2(_bankDb, query_stmt, -1, &statement, NULL) != SQLITE_OK)
        NSAssert(0, @"prepare failed: %s", sqlite3_errmsg(_bankDb));

    if (sqlite3_bind_text(statement, 1, [_usernameTextField.text UTF8String], -1, NULL) != SQLITE_OK)
        NSAssert(0, @"bind failed: %s", sqlite3_errmsg(_bankDb));

    int rc = sqlite3_step(statement);
    if (rc == 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 if (rc == SQLITE_DONE) {
        _status.text=@"match not found";
    } else {
        NSAssert(0, @"step failed: %s", sqlite3_errmsg(_bankDb));
    }
    sqlite3_finalize(statement);

    sqlite3_close(_bankDb);
}

If you're still failing with match not found, you should:

  • examine the contents of _usernameTextField.text to make sure your IBOutlet is hooked up correctly.

  • look at the contents of the database and make sure a record with the desired userid is found; you haven't shown us where you add the userid, so it's hard for us to diagnose why the userid in question was not found.

I must confess that the overall logic (just looking for matching records for that userid and populating the password field if you found the userid) looks highly suspect (you shouldn't be storing passwords in plaintext, you certainly shouldn't be returning a password provided simply a userid), but I'm focusing solely on the tactical issue of why you're seeing match not found error message.

于 2013-08-23T02:27:46.483 回答