我創建了一個應用程序。我的應用程序中的 我在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;
}
你認爲問題是什麼?你已經嘗試了什麼? – 2013-05-02 12:20:23
我的問題是我不知道如何在NSArray或NSDictionary(故事板)中存儲表sqlite的值! – emma 2013-05-02 12:22:38
你讀過SQLite教程嗎?你讀過一個UITableView教程嗎?你不應該指望別人爲你寫代碼。 – 2013-05-02 12:30:20