在Uitableview中显示Sqlite数据

时间:2012-08-28 11:37:59

标签: ios uitableview sqlite

抱歉,我自己试了一下,但找不到解决办法 我在viewdidload方法中有以下代码

NSString *docsDir;
NSArray *dirPaths;

dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
docsDir=[dirPaths objectAtIndex:0];
databasePath=[[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"database.db"]];

NSLog(@"%@",databasePath);
NSFileManager *filemgr=[NSFileManager defaultManager];

if([filemgr fileExistsAtPath:databasePath]==YES)
{


[self getFinal];
}
else {
    NSLog(@"please order");
}
[super viewDidLoad];

它调用一个名为getFinal的方法 如下

 sqlite3_stmt  *statement;

const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath,&database)==SQLITE_OK)
{
    NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM FINALORDER"];

    const char *query_stmt = [querySQL UTF8String];     
    sqlite3_prepare_v2(database,query_stmt,-1,&statement,NULL);

    if (sqlite3_step(statement) == SQLITE_ROW)
    {
        NSString *itemname = [[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]autorelease];
        //itemn.text = itemname;

        NSString *qua = [[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)]autorelease];
        //quantity.text = qua;

        NSString *total = [[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)]autorelease];
        //totalcost.text=total;
    }


}
    sqlite3_finalize(statement);
    sqlite3_close(database);

我想以tableview格式显示itemname,qua和total。我已经尝试过使用协议并实现委托以及数据源方法,但数据源方法cellforindexpath无法识别这些变量,我该怎么做呢?谢谢 这是表视图的代码

 - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
   {
  //I DONT KNOW WHAT TO RETURN HERE..
   }

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

// Set up the cell...
cell.textLabel.text = itemname  //i need to get the itemname here
cell.detailTextLabel.text = qua,total //i need the quantity and the totalcost values   

return cell;

}

5 个答案:

答案 0 :(得分:0)

为了清晰和简洁,您应该使用核心数据或至少FMDB作为您和数据库之间的层。

现在对于TableView问题,如果你也可以发布一些代码就好了。

答案 1 :(得分:0)

您已将itemName等声明为数据源方法不可见的范围内的局部变量。您需要将它们保存为视图控制器的实例变量。

答案 2 :(得分:0)

您可以使用代码:

NSString *itemname, *qua, *total;

内幕功能:

sqlite3_stmt  *statement;
const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, & database) == SQLITE_OK) {
    NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM FINALORDER"];
    const char *query_stmt = [querySQL UTF8String];

    if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK) {

       while (sqlite3_step(statement) == SQLITE_ROW) {

          itemname = [[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]autorelease];
          qua = [[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)]autorelease];
          total = [[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)]autorelease];
       }
    }
sqlite3_finalize(statement);
}
sqlite3_close(database);
[tblView reloadData];

答案 3 :(得分:0)

为此,你已经拿了一个数组。然后在while循环中添加代码 例如

listOfItems = [[NSMutableArray alloc] init]; 
[listOfItems addObject:itemname];

然后,方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [listOfItems count]; }

答案 4 :(得分:0)

Please Try This one it might be help you

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