Reputation: 531
{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
//array=[[NSMutableArray alloc]init];
//array1=[[NSMutableArray alloc]init];
// Build the path to the database file
databasePath = [docsDir stringByAppendingPathComponent: @"first.database"];
NSLog(@"%@",databasePath);
NSFileManager *fn=[NSFileManager defaultManager];
NSError *error;
BOOL success=[fn fileExistsAtPath:databasePath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"first.database"];
success = [fn copyItemAtPath:defaultDBPath toPath:databasePath error:&error];
}
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT firstname FROM second"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
arrival = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,0)];
NSLog(@"Result %@",arrival);
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
[self.tableView reloadData];
}
}
In this Example i want to display "arrival" value to UITableView... Anyone Help me
Upvotes: 1
Views: 764
Reputation: 1116
First of all - use FMDB wrapper for sqlite databases. It makes life so much easier! (check their GitHub here and this tutorial) Second - if you want to assign result of SELECT to cell just use:
cell.textLabel.text = your_result;
in -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Upvotes: 1
Reputation: 7333
In cellForRowAtIndexPath Method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NULL];
cell.textLabel.text=arrival;
return cell;
}
Upvotes: 0