(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クエリが実行されない理由がわかりませんでした。解決策を教えてください。