如何从iphone中的sqlite数据库中获取数据

时间:2013-01-10 11:31:29

标签: iphone objective-c arrays sqlite

我有sqlite数据库,数据库格式是

表名=主项目

column 0(M_ID) column 1(M_ISSUE)
M1               i1
M1               i2
M1               i3
M2               x
M2               y 
M2               z
M3               1 
M3               2 

在上表中,我需要显示M_ID问题计数,(例如: - M_ID(M1)有3个问题(i1,i2,i3),如明智的M2和M3。

这是我的源代码

for (int i = 0; i < [masterProjIDArray count]; i++)
{
    MySQLite *sqlobj = [[MySQLite alloc] initWithSQLFile:@"myDb"];
    [sqlobj openDb];
    NSString *query = [NSString stringWithFormat:@"SELECT *FROM MASTER_PRJ_DETAILS WHERE M_ID = \'%@\'", [masterProjIDArray objectAtIndex:i]];
    NSLog(@"SQL query:%@", query);
    [sqlobj readDb:query];

    while ([sqlobj hasNextRow])
    {
        NSString *mProjIssuesID = [sqlobj getColumn:1 type:@"text"];

        [masterProjIssueIDArray addObject:mProjIssuesID];
    }
    NSLog(@"issueCount:%@", masterProjIssueIDArray);

    [sqlobj closeDb];
}
[sqlobj release];

请给我一些关于显示个人数量M_ID问题的建议。

3 个答案:

答案 0 :(得分:0)

NSLog(@“issueCount:%d”,[masterProjIssueIDArray count]);

答案 1 :(得分:0)

有两种方法可以计算

使用数组计数

[masterProjIssueIDArray count]

或在数据库中计数

SELECT count(*) FROM MASTER_PRJ_DETAILS

希望它可以帮到你

答案 2 :(得分:0)

        MySQLite *sqlobj = [[MySQLite alloc] initWithSQLFile:@"myDb"];
        [sqlobj openDb];
        NSString *query = [NSString stringWithFormat:@"SELECT *FROM MASTER_PRJ_DETAILS WHERE M_ID = '%@'",M1];

      NSLog(@"SQL query:%@", query);
     NSMutableArray *arrydata= [sqlobj readDb:query];             
       //You got 3 records...


         for (int i = 0; i < [arrydata count]; i++)
         {
               NSMUtableDisctionry *dist=[arrydata objectAtIndex:i];
               NSString *strIsuue=[dist objectforkey:@"M_ISSUE"];
               [masterProjIssueIDArray addObject:strIsuue];

               NSLog(@"issueCount:%@", masterProjIssueIDArray);


  } 
  [sqlobj closeDb];
  [sqlobj release];

我希望它的工作......