在iphone上的sqlite问题

时间:2010-12-24 05:00:40

标签: iphone sqlite

我可以执行查询,但我可以看到 list_ID 的唯一最后一个值

我想访问4但我得到6 ???

list_ID is ----------------------->is 4
2010-12-24 12:57:07.507 DatabaseTest[3398:207]  QUERY EXECUTION
2010-12-24 12:57:07.508 DatabaseTest[3398:207] bxbxbxmnb
2010-12-24 12:57:07.508 DatabaseTest[3398:207]  list_ID is ----------------------->is 5
2010-12-24 12:57:07.509 DatabaseTest[3398:207]  QUERY EXECUTION
2010-12-24 12:57:07.510 DatabaseTest[3398:207] bxbxbxmnb
2010-12-24 12:57:07.511 DatabaseTest[3398:207]  list_ID is ----------------------->is 6

这是代码

        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
        {
            while(sqlite3_step(compiledStatement) == SQLITE_ROW)

            {


                acat=[[[Cat_tableList alloc]init]autorelease];



                /////////////  statement////////

            NSLog(@" QUERY EXECUTION");




                                //  sStudent.cat_id=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];

                acat.list_id=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                acat.cat_id=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

                acat.names=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                acat.content=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];








                [appDelegate.catLists addObject:acat];
(@"final AUTo_ID array is  ............%@",acat.content);
                NSLog(@" list_ID is ----------------------->is %@",acat.list_id);  i just want value to b 4 so that i can use this somewhere
                ///////////////


            }

4 个答案:

答案 0 :(得分:0)

您没有显示您的sqlite查询语句,但确定....

此处您正在使用Cat_tableList类的4个对象,其值(指针值)对于每个数据库表的行运行的每个循环值都会发生变化.....所以出现此问题是因为最后一条记录可能是list_ID = 6 ..

我建议

  1. 使用array of arrayarray of dictionaries存储所有这些,然后您可以使用其中任何一个......
  2. 你也可以modify your sqlite request(我不知道你在这里使用了什么我认为你使用了SELECT * FROM Table1)....修改它并改为SELECT * FROM Table1 WHERE list_ID=4; (list_ID应替换为数据库表中列的确切名称)..
  3. 修改

    如果你想要确切的id = 4,你可以使用这样的东西

    while(sqlite3_step(compiledStatement) == SQLITE_ROW)
    
                {
    
    if([[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)] isEqualToString:@"4"])
    {
                    acat=[[[Cat_tableList alloc]init]autorelease];
    
    
                    acat.list_id=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                    acat.cat_id=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
    
                    acat.names=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                    acat.content=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
    
    
    
    
    
                    [appDelegate.catLists addObject:acat];
    (@"final AUTo_ID array is  ............%@",acat.content);
                    NSLog(@" list_ID is ----------------------->is %@",acat.list_id);
    }
    
    
    
       }
    

    修改--- 2

    如果您想要表格的所有值

    ,请考虑以下示例
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
                        {
    
                    NSMutableArray *arr=[[NSMutableArray alloc]init];
    
    
    
    NSString *str1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                        [arr addObject:str1];
    NSString *str2 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                        [arr addObject:str2];
    NSString *str3 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                        [arr addObject:str3];
    
                    [appDelegate.catLists addObject:arr];
                    [arr release];
                }   
    

    现在catLists拥有数据库的所有值

    现在通过

    访问它
    [[appDelegate.catLists objectAtIndex:int1]objectAtIndex:int2];
    

    希望它可以帮到你

答案 1 :(得分:0)

首先我这样做是为了获取任何行的当前索引

appDelegate.a1 = [[NSMutableArray alloc] init];// a1 is new array 


            while(sqlite3_step(compiledStatement) == SQLITE_ROW)

            {
         acat.list_id=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];

                NSArray  * myArray2 = [NSArray arrayWithObjects:acat.list_id,nil];




                [appDelegate.a1 addObjectsFromArray:myArray2];





                [appDelegate.a1 addObject:myArray2];



}

然后在其他类中获取list_id值我正在执行此操作

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic -- create and push a new view controller


{
rowID=[[appDelegate.a1 objectAtIndex:0]intValue] +[indexPath row];
[self calldatabase];
}



void(calldatabase)
{
 NSString *query=  [[NSString alloc] initWithFormat:@"select * from list_tbl where cat_id=%@ and list_id=%d",aCat.cat_id, rowID ];// here rowid starts from 4 and keep inc by 1

}
}

///这是有效但我想知道这是好方法,不会消耗太多内存??? PLZ建议 感谢

答案 2 :(得分:0)

以这种方式执行代码

调用sqlite并将所有数据存储在appdelegate数组中,然后根据需要使用

如果你想从阵列中选择4,5 6中的数据然后使用这段代码,你只需要选择有条件的数据

newarray = [[nsmutablearray alloc] init]; for(int = 3; i< [appdelegatearray count]; i ++) {    [newarray addobject:[appdelegatearray objectatindex:i]; }

通过这种方式,你可以做任何希望对你有所帮助的事情,让你更清楚地告诉你问题我觉得你害怕分享

答案 3 :(得分:0)

首先感谢所有帮助过我的人,因为我的问题已得到解决

这就是我这样做的方式

rowID=[[appDelegate.a1 objectAtIndex:rowPointer]intValue];, where rowPointer=[indexPath row];

首先我将所有值存储在数组中,然后通过索引访问它们: - )