为什么找不到SQLite记录?

时间:2013-08-23 01:30:50

标签: objective-c login passwords username

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

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

}

1 个答案:

答案 0 :(得分:4)

你应该做以下事情,

  1. 如果任何查询失败,请查看sqlite3_errmsg值(例如sqlite3_prepare_v2未返回SQLITE_OKsqlite3_step未返回SQLITE3_DONE或{ {1}});

  2. 请勿在查询中使用SQLITE3_ROW,而应使用stringWithFormat占位符并将值绑定到?

  3. 因此:

    sqlite3_bind_text

    如果您仍然未能使用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); } ,则应该:

    • 检查match not found的内容,确保您的_usernameTextField.text正确连接。

    • 查看数据库的内容,确保找到包含所需用户标识的记录;你没有向我们展示你添加用户标识的位置,所以我们很难诊断为什么没有找到有问题的用户标识。

    我必须承认整体逻辑(只是查找该用户ID的匹配记录并填写密码字段,如果找到用户ID)看起来非常可疑(你不应该以明文存储密码,你当然不应该返回一个只提供用户ID的密码,但我只关注你为何看到IBOutlet错误信息的战术问题。