如何通过从ios中的数据库中获取数据来将数据填充到UItableView中

rak*_*esh -2 uitableview ios

直到现在我已经使用数组将数据插入到IOS中的表中,现在我想使用数据库(sqlite),我需要从该数据库获取数据并将该数据插入表中.请帮我.

提前致谢.

Bha*_*esh 5

如果您是sqlite的初学者并想要学习它,请转到下面的链接.

1)IOS Sqlite3和db中的数据库

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

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

我还建议你学习FMDB(sqlite包装器).

编辑:

假设你有数据库存储学生的数据然后首先获取相应数组中的数据,如,

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];
}
Run Code Online (Sandbox Code Playgroud)

假设您有一个名为"tblStudent"的表,那么您的数据源方法将如下所示

  -(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;
  }
Run Code Online (Sandbox Code Playgroud)