-2

Untill now I have used arrays to insert data into the tables in IOS, now I want to use a database(sqlite), from that database I need to fetch the data and insert that data into table. please help me.

Thanks in advance.

Nigel B
  • 3,577
  • 3
  • 34
  • 52
rakesh
  • 73
  • 3
  • 12

1 Answers1

5

If you are beginner for sqlite and want to learn it then go to below links.

1)Database in IOS Sqlite3 and db

2)http://www.apptite.be/tutorial_ios_sqlite.php

3)http://maniacdev.com/2011/11/tutorial-easy-ios-databases-with-sqlite-and-fmdb

I also suggest you to learn FMDB (wrapper for sqlite).

EDIT:

Suppose you have database which stores the data of students then first of all fetch those data in the corresponding arrays like,

sqlite3_stmt *statement;

NSString *selectSQL = @"SELECT * FROM student";

const char *insert_stmt = [selectSQL UTF8String];
if(sqlite3_prepare_v2(studentData, insert_stmt,  -1, &statement, NULL) == SQLITE_OK)
{
    while(sqlite3_step(statement) == SQLITE_ROW)
    {

        [arrFirstName addObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)]];

        [arrMiddleName addObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)]];

        [arrLastName addObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 2)]];

        [arrContactNo addObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 3)]];

    }

}
[tblStudent reloadData];
}

Suppose You have table named "tblStudent" then your data source methods will look like this

  -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
      return 1;
  }

  -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
        return [arrFirstName count];
  }

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
         UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
         cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",[arrFirstName  objectAtIndex:indexPath.row],[arrLastName objectAtIndex:indexPath.row]];
         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
         return cell;
  }
Community
  • 1
  • 1
Bhavesh
  • 1,422
  • 1
  • 12
  • 31