2

抱歉,我自己尝试过,但找不到解决方案我的 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;

}

4

5 回答 5

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];
于 2012-08-28T12:15:16.580 回答
0

为此,您需要一个数组。然后添加到while循环中的代码,例如。

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

然后,方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [listOfItems count]; }
于 2012-08-28T12:29:26.083 回答
0

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

于 2012-08-28T11:58:15.897 回答
0

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

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

于 2012-08-28T11:45:54.717 回答
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;
    }
于 2017-04-04T07:33:26.680 回答