【发布时间】:2013-04-26 13:39:12
【问题描述】:
我创建了一个应用程序。 在我的应用程序中,我将 2 个数组存储在 sqlite 表数据库中,但我无法从 UItableView 中的 sqlite 显示。 我在谷歌搜索,但没有找到关于 sqlite 故事板中的显示数据。!!! 请指导我如何在 tableview 中显示来自 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;
}
【问题讨论】:
-
你认为问题是什么?你已经尝试过什么?
-
我的问题是我不知道如何将表 sqlite 中的值存储在 NSArray 或 NSDictionary(在故事板中)!!!
-
你读过 SQLite 教程吗?你读过 UITableView 教程吗?你不应该期望别人为你写代码。
-
是的,但我很困惑!!!我的朋友请用代码告诉我
标签: ios objective-c sqlite uitableview storyboard