我正在尝试在视图控制器中编写用户名/密码日志,在我的应用程序中没有错误,没有警告,但它总是输出“匹配未找到”,即使我选择了我的数据库中已存在的用户名。如果你能提供帮助,我真的很感激 这是代码:
[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);
}
}
答案 0 :(得分:4)
你应该做以下事情,
如果任何查询失败,请查看sqlite3_errmsg
值(例如sqlite3_prepare_v2
未返回SQLITE_OK
或sqlite3_step
未返回SQLITE3_DONE
或{ {1}});
请勿在查询中使用SQLITE3_ROW
,而应使用stringWithFormat
占位符并将值绑定到?
因此:
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
错误信息的战术问题。