1

我在将我的文本字段电子邮件值与 sqlite 数据库中的值进行比较时遇到问题。如果两个值相同,则会显示“数据库中已存在电子邮件”的警报框。为此,我创建了一个名为 findcontact 的函数,我通过单击按钮调用该函数。按钮单击方法正在工作,因为当我单击按钮时,按钮单击方法会生成我在 findContact 方法中编写的日志。

但对我来说问题是如果两封电子邮件都匹配,如何比较电子邮件并显示警报框

- (void) findContact:(NSString *) email{

    const char *dbpath = [path UTF8String];

    if (sqlite3_open(dbpath, &database) == SQLITE_OK){
        NSString *querySQL = [NSString stringWithFormat: @"SELECT email FROM CONTACT WHERE email=\"%@\"",email];
        NSLog(@" email = %@", querySQL);
        const char *query_stmt = [querySQL UTF8String];
        NSLog(@" char email = %s", query_stmt);
        if (sqlite3_prepare_v2(database, query_stmt, -1, &init_statement, NULL) == SQLITE_OK){
            if (sqlite3_step(init_statement) == SQLITE_ROW){
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"email already exist in database" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK ", nil];
                [alert show];
                [alert release];
            }
            else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"email not exist" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK ", nil];
                [alert show];
                [alert release];
            }
            sqlite3_finalize(init_statement);
        }
        sqlite3_close(database);
    }
}
4

2 回答 2

1

在您的代码中进行这样的比较

    while(sqlite3_step(compiledStatement)==SQLITE_ROW)
    {

        NSString *emaiID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,0)];

        if ([emaiID isEqualToString:yourTextField.text])
        {
            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:@"email already exist in database"
                                  delegate:nil
                                  cancelButtonTitle:nil
                                  otherButtonTitles:@"OK ", nil];
            [alert show];
        }
        else{
            //Your task
        }
        }
于 2012-12-05T08:12:06.950 回答
0

检查这个

NSString *querySQL = [NSString stringWithFormat: @"SELECT email FROM CONTACT WHERE email='%@'",email]; 

    if(sqlite3_prepare_v2(sharedDataBase, [querySQL UTF8String], -1, &statment, nil) == SQLITE_OK)
    {
        if(sqlite3_step(statment)==SQLITE_ROW){

            char *email=(char *)sqlite3_column_text(statment, 0); 

            NSString  *emailStr =[NSString stringWithUTF8String:email]; 

            // email exist 

        }
        else{

            // email not exist
        }

        sqlite3_finalize(statment); 
    }
    else
        sqlite3_close(sharedDataBase); 
于 2012-12-05T07:51:35.857 回答