2013-05-02 42 views
4

我创建了一个应用程序。我的应用程序中的 我在sqlite表DataBase中存储了2个数组,但我无法从UItableView中的sqlite中显示。 我在谷歌搜索,但没有发现有关从sqlite故事板显示数据。 请指导我如何在tableview中显示来自sqlite数据库的数据。 这是我的代码:如何在Storyboard中的UITableView中显示来自sqlite的数据

#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; 
} 
+0

你认为问题是什么?你已经尝试了什么? – 2013-05-02 12:20:23

+0

我的问题是我不知道如何在NSArray或NSDictionary(故事板)中存储表sqlite的值! – emma 2013-05-02 12:22:38

+1

你读过SQLite教程吗?你读过一个UITableView教程吗?你不应该指望别人为你写代码。 – 2013-05-02 12:30:20

回答

4

创建一个方法,它将获取存储在sqlite中的所有播放器,如findAllPlayers。创建一个dataSourceArray,它将保存从前一个方法返回的玩家。为了便于使用,可以很容易地形成模型自定义对象而不是字典。

//Player.h 

@interface Player : NSObject 

@property (nonatomic) NSUInteger playerID; 
@property (nonatomic, copy) NSString *name; 
@property (nonatomic, copy) NSString *team; 

实用程序PlayerDatabase,用于从sqlite插入和提取播放器实例。

//PlayerDatabase.h 

@class Player; 
@interface PlayersDatabase : NSObject 

+ (PlayersDatabase*)database; 
- (NSArray *)findAllPlayers; 
- (BOOL)insertPlayer:(Player *)player; 

//PlayerDatabase.m 
@implementation PlayersDatabase 

static PlayersDatabase *_database; 

+ (PlayersDatabase*)database { 
    if (_database == nil) { 
     _database = [[PlayersDatabase alloc] init]; 
    } 
    return _database; 
} 

- (id)init { 
    if ((self = [super init])) { 

     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     NSString *filePath = [self databasePath]; 
     if (![fileManager fileExistsAtPath:filePath]) { 
      NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"Players" 
                   ofType:@"db"]; 
      [fileManager copyItemAtPath:sqLiteDb 
           toPath:filePath 
            error:NULL]; 
     } 

    } 
    return self; 
} 

- (NSString *)databasePath 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = paths[0]; 
    return [documentsDirectory stringByAppendingPathComponent:@"Players.db"]; 
} 

- (NSArray *)findAllPlayers 
{ 
    NSMutableArray *players = [NSMutableArray array]; 
    FMDatabase *database = [FMDatabase databaseWithPath:[self databasePath]]; 
    [database open]; 

    FMResultSet *resultSet = [database executeQuery:@"SELECT * from Player ORDER BY Name"]; 

    while ([resultSet next]) { 

     Player *player = [[Player alloc]init]; 

     player.playerID = [resultSet intForColumn:@"PlayerID"]; 
     player.name  = [resultSet stringForColumn:@"Name"]; 
     player.team  = [resultSet stringForColumn:@"Team"]; 

     [players addObject:player]; 

    } 

    [database close]; 

    return players; 
} 

- (BOOL)insertPlayer:(Player *)player 
{ 
    FMDatabase *db = [FMDatabase databaseWithPath:[self databasePath]]; 
    [db open]; 
    BOOL success = [db executeUpdate:@"INSERT INTO Player (Name,Team) VALUES (?,?);",player.name,player.team, nil]; 
    [db close]; 
    return success; 
} 
与的tableView的的viewController

现在,第一填充源码,然后重新加载的tableView

//YourViewController.h 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSArray *names = [NSArray arrayWithObjects:@"Ribery",@"Ronaldo",@"Messi",@"Zannati",@"Totti", nil]; 
    NSArray *teams = [NSArray arrayWithObjects:@"byern",@"R.Madrid",@"Barcelona",@"Inter",@"Rome", nil]; 
    for (int i = 0; i < [Name count]; i++) 
    { 
     Player *player = [[Player alloc]init]; 

     player.name = names[i]; 
     player.team = teams[i]; 

     [[PlayerDatabase database] insertPlayer:player]; 
    } 

    self.players = [self findAllPlayers]; 
    [self.tableView reloadData]; 

} 

#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 [self.players count]; 
} 
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 
    //Here the dataSource array is of dictionary objects 
    Player *player = self.players[indexPath.row]; 
    cell.textLabel.text = player.name; 
    cell.detailTextLabel.text = player.team; 

    return cell; 
} 

我制定了上述的教程下面的示例项目,该数据被预装到源码。所以你可能需要调整它的使用。

Source Code

+0

我的朋友你能告诉我什么是findAllPlayers方法?我不知道它 – emma 2013-05-02 12:11:04

+0

我的朋友告诉我更多关于findAllPlayers,请用代码 – emma 2013-05-02 12:19:53

+0

@emma我已经更新了我的答案。 – Anupdas 2013-05-02 12:25:04

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; 
} 
+0

我的朋友“DatabaseKeys”&“emp.Eno”是什么? – emma 2013-05-02 11:57:59

+0

我的编译器未知。 – emma 2013-05-02 11:59:49

+0

我的朋友“DatabaseKeys”&“emp.Eno”是什么? – emma 2013-05-02 12:16:55

相关问题