4

1 つのアプリケーションを作成します。私のアプリケーションでは、SQLite テーブル DataBase に 2 つの配列を格納していますが、SQLite から UItableView に表示できません。Googleで検索しましたが、sqliteのストーリーボードの表示データについて見つかりませんでした.!!! テーブルビューでsqlite DBからのデータを表示する方法を教えてください。これは私のコードです:

#import "ViewController.h"
#define DataName @"DB.sqlite"

@implementation ViewController
{
    NSDictionary * dictionary;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *Name = [NSArray arrayWithObjects:@"Ribery",@"Ronaldo",@"Messi",@"Zannati",@"Totti", nil];
    NSArray *Team = [NSArray arrayWithObjects:@"byern",@"R.Madrid",@"Barcelona",@"Inter",@"Rome", nil];
    for (int i = 0; i < [Name count]; i++)
    {
        NSString * name = [Name objectAtIndex:i];
        NSString * team = [Team objectAtIndex:i];
        dictionary = [NSDictionary dictionaryWithObjectsAndKeys:name,@"Name",team,@"Team",nil];
        NSString *query = [NSString stringWithFormat:@"insert into tableFootball (Name,Team) values('%@','%@')",[dictionary objectForKey:@"Name"],[dictionary objectForKey:@"Team"]];
        [self executeQuery:query];
    }

}
-(NSString *) dataFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"PATH %@",[documentsDirectory stringByAppendingPathComponent:DataName]);
    return [documentsDirectory stringByAppendingPathComponent:DataName];
}
-(void)executeQuery:(NSString *)query
{
    //NSLog(@"QUERY : %@",query);

    sqlite3_stmt *statement;
    if(sqlite3_open([[self dataFilePath] UTF8String], &DataBase) == SQLITE_OK)
    {
        if (sqlite3_prepare_v2(DataBase, [query UTF8String], -1, &statement, NULL) == SQLITE_OK)
        {
            if (sqlite3_step(statement) != SQLITE_DONE)
            {
                sqlite3_finalize(statement);
            }
        }
        else
        {
            NSLog(@"query Statement Not Compiled");
        }

        sqlite3_finalize(statement);
        sqlite3_close(DataBase);
    }
    else
    {
        NSLog(@"Data not Opened");
    }
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 1;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //I dont know what put here for display from sqlite
    return cell;
}
4

2 に答える 2

0

データベース sqlite からデータを取得するには....次のように記述する必要があります....そして、コードに従っていくつかの変更を加えます....

+(NSMutableArray*)getData
{
    NSString *strDestPath = [NSString stringWithFormat:@"%@/Documents/DBEmployee.sqlite",NSHomeDirectory()];
    sqlite3 *db;
    NSMutableArray   *empArr = [[NSMutableArray alloc]init];
    if(sqlite3_open([strDestPath UTF8String] , &db)  == SQLITE_OK)
    {
        NSString *query = @"SELECT * FROM Employee";
        char* err_msg;
        sqlite3_stmt* statement;
        if(sqlite3_prepare_v2(db, [query UTF8String], -1,&statement, &err_msg) == SQLITE_OK)
        {
            while (sqlite3_step(statement) == SQLITE_ROW) {
                DatabaseKeys *emp = [[DatabaseKeys alloc]init];
                emp.Eno = sqlite3_column_int(statement, 0);
                NSString *strn = [NSString stringWithFormat:@"%d",emp.Eno];
                [empArr addObject:strn];


            }
        }
    }
    return empArr;
}
于 2013-05-02T11:51:33.167 に答える