0

我在我的 iphone 应用程序中使用 SQLite3 做一个登录表单。用户名和密码已成功上传到数据库,但是当我检查数据是否存在时出现错误。这是我在尝试捕获数据时使用的代码:

- (IBAction)loginTapped:(id)sender
{
if (sqlite3_open([self.filePath UTF8String], &db) == SQLITE_OK)
{
    NSString *sql = [NSString stringWithFormat:@"SELECT username, password FROM register WHERE username = '%@'",[self.usernameLogin text]];
    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(db, sql.UTF8String, -1, &statement, NULL) == SQLITE_OK)
    {
        if (sqlite3_step(statement) == SQLITE_ROW)
        {
            char *f1 = (char *)sqlite3_column_text(statement, 1);
            NSString *user = [[NSString alloc] initWithUTF8String:f1];
            char *f2 = (char *)sqlite3_column_text(statement, 2);
            NSString *pass = [[NSString alloc] initWithUTF8String:f2];
            NSLog(@"User : %@, Password : %@",user, pass);
        }
    }

    sqlite3_finalize(statement);
}
sqlite3_close(db);
}

调用此方法时,我收到此错误:

2013-11-09 22:34:24.702 SQLiteTest[1385:60b] *** Terminating app due to uncaught exception     'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithUTF8String:]: NULL  cString'
*** First throw call stack:
(0x2f5eef53 0x399c76af 0x2f5eee95 0x2ff24f87 0xf7789 0x31d94f3f 0x31d94edf 0x31d94eb9   0x31d80b3f 0x31d9492f 0x31d94601 0x31d8f68d 0x31d64a25 0x31d63221 0x2f5ba18b 0x2f5b965b  0x2f5b7e4f 0x2f522ce7 0x2f522acb 0x34243283 0x31dc4a41 0xf7d25 0x39ecfab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

有什么问题?

4

2 回答 2

0

在从中创建字符串之前添加检查来自数据库的值不为空。喜欢:

NSString *user;
NSString *pass;

char *f1 = (char *)sqlite3_column_text(statement, 1);
if(f1 != NULL)
    user = [[NSString alloc] initWithUTF8String:f1];

char *f2 = (char *)sqlite3_column_text(statement, 2);
if(f2!= NULL)
     pass = [[NSString alloc] initWithUTF8String:f2];

NSLog(@"User : %@, Password : %@",user, pass);
于 2013-11-10T05:35:40.400 回答
0

列索引从零开始:

        char *f1 = (char *)sqlite3_column_text(statement, 0);
        char *f2 = (char *)sqlite3_column_text(statement, 1);
于 2013-11-09T22:19:57.770 回答