如何将我的电子邮件文本字段值与iphone中的数据库值进行比较

时间:2012-12-05 07:38:23

标签: iphone objective-c ios

我在将文本字段电子邮件值与sqlite数据库中的值进行比较时遇到问题。如果两个值相同,则显示警告框“电子邮件已存在于数据库中”。我为此创建了一个名为findcontact的函数,我从按钮单击调用该函数。按钮单击方法正在工作,因为当我单击按钮然后buttonclick方法生成我在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);
    }
}

2 个答案:

答案 0 :(得分: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
        }
        }

答案 1 :(得分: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);