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

この例では、「arrival」値を UITableView に表示したい... 誰でも助けて

4

2 に答える 2

1

まず、sqlite データベースには FMDB ラッパーを使用します。それは人生をとても簡単にします!(こちらの GitHubこのチュートリアルを確認してください) 2 つ目 - 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 に答える