user3427551
user3427551

Reputation: 345

One record is not inserting properly into sqlite3 database IOS7

In my application I'm using sqlite3 database and I'm storing the data in to the database. I have 4 fields like name,city,phone and email id the problem is the city field is not inserting into the database instead of that its taking the phone number data in city field.

      -(void) createTable: (NSString *) tableName
         withField1:(NSString *) field1
         withField2:(NSString *) field2
         withField3:(NSString *) field3
         withField4:(NSString *) field4;
     {
         char *err;
         NSString *sql =[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' TEXT, '%@' TEXT, '%@' TEXT, '%@' TEXT);",tableName,field1,field2,field3,field4];
      if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
         sqlite3_close(db);
         NSAssert(0, @"Could not create table");
      }else{
         NSLog(@"Table Created");
      }  
     }

Database Creation.

    [self createTable:@"reg" withField1:@"nam" withField2:@"cit" withField3:@"ph" withField4:@"em"];

IBOutlets.

@synthesize name,citiy,phone,email;

Inserting the data into database.

  - (IBAction)save:(id)sender {
         NSString *namm = name.text;
         NSString *citt = citiy.text;
         NSString *phon = phone.text ;
         NSString *eml = email.text;

         NSString *sql = [NSString stringWithFormat:@"INSERT INTO reg ('nam','cit','ph','em') VALUES ('%@','%@','%@','%@')",namm,citt,phon,eml];

         char *err;
     if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
        sqlite3_close(db);
        NSAssert(0, @"Could not create table");
     }else{
         NSLog(@"Table Updated");
     }
          name.text=@"";
          citiy.text=@"";
          phone.text=@"";
          email.text=@"";
     }

In the above i have checked everything but I'm not able to find where I'm doing wrong in this code. Please tell me where I'm doing wrong. Its showing like this in my database.

Thanks.

enter image description here

Upvotes: 0

Views: 111

Answers (1)

V-Xtreme
V-Xtreme

Reputation: 7333

You code seems to be fine . Though I am guessing just try to print city and phon

     NSString *citt = citiy.text;
     NSString *phon = phone.text ;

If you are getting the wrong data here in then check your connection you might done wrong with your connections(IBOutlet).

Upvotes: 1

Related Questions