0
(void)addPersonButton:(id)sender {
            char *error;
            if (sqlite3_open([dbPathString UTF8String], &personDB) == SQLITE_OK) {
                if ([nameField.text length] > 0 && [ageField.text length] > 0 ){
                //{NSLog(@"mobile num %@", mobileNumField.text);
                    //NSCharacterSet *decimalSet = [NSCharacterSet decimalDigitCharacterSet];

                    //if ([mobileNumField.text rangeOfCharacterFromSet:decimalSet].location == NSNotFound) {

                NSString *phoneNum = @"^(\\+91||0)?\\d{10}";

                    NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES  %@", phoneNum];

                    BOOL matches = [test evaluateWithObject:[mobileNumField text]];
                    BOOL valid;
                    if (matches) {
                     sqlite3_stmt *statement;
                     NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM PERSONS"];

                     const char *query_sql = [querySQL UTF8String];

                     if (sqlite3_prepare(personDB, query_sql, -1, &statement, NULL) == SQLITE_OK) {
                     while (sqlite3_step(statement) == SQLITE_ROW) {
                     NSString *mobilenum = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
                         NSLog(@"in DB: %@ and in TF:%@", mobilenum, mobileNumField.text);
                         NSInteger len = [mobileNumField.text length];

                         NSLog(@"len %d",len);
                        // if (len > 10) {
                             NSInteger len1 = len - 10;
                             NSString *string = [mobileNumField.text substringWithRange:NSMakeRange(len1, 10)];
                         NSRange find = [mobilenum rangeOfString:string];
                         NSLog(@"find length %d",find.length);
                         //}
                         if (find.length) {
                             valid = false;
                             break;
                         }//else {NSLog(@"am in else");
                             valid = true;
                             //break;
                         //}
                     }NSLog(@"am out of while");

                         if (valid) {
                             NSLog(@"am inside valid if");
                             NSLog(@"%@", mobileNumField.text);
                    NSString *insertStmt = [NSString stringWithFormat:@"INSERT INTO PERSONS(NAME,AGE,MOBNUM) values ('%s','%d','%s')" ,[self.nameField.text UTF8String], [self.ageField.text intValue],[self.mobileNumField.text UTF8String]];         
                    const char *insert_stmt = [insertStmt UTF8String];
                             NSLog(@"insertStmt=%@ and insert_stmt = %s",insertStmt,insert_stmt);
                             int flag1 = (sqlite3_prepare(personDB, query_sql, -1, &statement, NULL) == SQLITE_OK);
                             int flag = (sqlite3_exec(personDB, insert_stmt, NULL, NULL, &error) == SQLITE_OK);
                             NSLog(@"flag1 = %d and flag = %d", flag1, flag);

                if (flag) {
                    NSLog(@"Person Added");

                    Person *person = [[Person alloc] init];

                    [person setName:self.nameField.text];
                    [person setAge:[self.ageField.text intValue]];
                    [person setNumber:self.mobileNumField.text];

                    [arrayOfPersons addObject:person];

                    nameField.text = @"";
                    nameField.placeholder = @"Name ";
                    ageField.text = @"";
                    ageField.placeholder = @"Age";
                    mobileNumField.text = @"";            
                }else NSLog(@"person not added");

                         }
                         else {
                             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Already registered with this mobile num" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
                             [alert show];
                         }

                     }

                }
                    else {
                        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Sorry, It's not valid mobile number. Please enter again" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];

                        [alert1 show];
                    }


                }
                else {

                    UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Name and Age fields are should not be empty" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];

                    [alert2 show];
                }
                sqlite3_close(personDB);

            }
        }

在上面的代码中,我将手机号码作为主键,所以每当我使用相同的手机号码注册时,它会在收到警报后显示“警报”,我会给出另一个之前未注册的手机号码,当时我的 SQL 查询(sqlite3_exec(personDB, insert_stmt, NULL, NULL, &error) == SQLITE_OK) 不执行。我不明白为什么我的 SQL 查询没有被执行。请给我解决方案。

4

2 回答 2

0

NSApplicationSupportDirectory 是最适合您的,将您的数据库放在 NSApplicationSupportDirectory 中,然后可以从那里访问数据,您可以执行插入和其他操作。:)

于 2013-01-31T07:11:05.673 回答
0

你启用ARC了吗?如果没有,您是否发布了警报?

于 2013-01-31T07:04:32.400 回答