1
{
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];
}
}

在此示例中,我想向 UITableView 显示“到达”值...任何人都帮帮我

4

2 回答 2

1

首先 - 对 sqlite 数据库使用 FMDB 包装器。它让生活变得如此轻松!(在此处查看他们的 GitHub本教程)其次 - 如果您想将 SELECT 的结果分配给单元格,只需使用:

cell.textLabel.text = your_result;

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

于 2012-09-03T11:31:57.980 回答
0

在 cellForRowAtIndexPath 方法中:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NULL];
    cell.textLabel.text=arrival;
    return cell;
}
于 2012-09-03T11:36:59.117 回答