检查数据库- SQLite3和objective-c中是否存在用户名。

时间:2021-07-19 00:04:56

Im doing a login form in my iphone app with SQLite3. The username and password are successfully uploaded to the database but when I check for the existence of the datas I get an error. This is the code I use when trying to catch the data:

我正在用SQLite3在我的iphone应用程序中做一个登录表单。用户名和密码成功地上载到数据库,但当我检查数据是否存在时,我就会出错。这是我试图捕捉数据时使用的代码:

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

When this method is called I get this error:

当这个方法被调用时,我得到这个错误:

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) 

What can be wrong?

什么是错误的吗?

2 个解决方案

#1


0  

Column indexes start at zero:

列索引从0开始:

        char *f1 = (char *)sqlite3_column_text(statement, 0);
        char *f2 = (char *)sqlite3_column_text(statement, 1);

#2


0  

Add a check that value came from database is not null before creating a string out of it. like:

在创建字符串之前,添加一个来自数据库的值不是null。如:

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

#1


0  

Column indexes start at zero:

列索引从0开始:

        char *f1 = (char *)sqlite3_column_text(statement, 0);
        char *f2 = (char *)sqlite3_column_text(statement, 1);

#2


0  

Add a check that value came from database is not null before creating a string out of it. like:

在创建字符串之前,添加一个来自数据库的值不是null。如:

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