从数据库中获取数据

时间:2013-08-26 16:41:10

标签: iphone sql database sqlite

我在服务器中存储了一些信息。我使用JSON来获取数据。数据被提取并存储到设备数据库并从中获取。问题是我在cat_id = 1&中有一些设置图像。一些设置图像在cat_id = 2。现在我使用不同的数组来处理不同的图像集,例如const char *sql = "SELECT id,cat_id,product_image FROM product where cat_id = '1'";const char *sql = "SELECT id,cat_id,product_image FROM product where cat_id = '2'";。如果我使用“%@”,它会显示所有值。

代码:

第一组图片:

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) { 

const char *sql = "SELECT id,cat_id,product_image FROM product where cat_id = '1'"; 

NSLog(@"sql is %s",sql); 

sqlite3_stmt *statement; 
// int catID = 0; 
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) { 
// We "step" through the results - once for each row. 
while (sqlite3_step(statement) == SQLITE_ROW) { 


catName = [[NSString alloc] initWithUTF8String: 
(const char *) sqlite3_column_text(statement, 2)]; 
NSLog(@"catName is %@",catName); 

[mArray addObject:catName]; 
[catName release]; 

// catID = sqlite3_column_int(statement, 0); 
} 
} 


sqlite3_finalize(statement); 
} 

else { 
sqlite3_close(database); 
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database)); 
// Additional error handling, as appropriate... 
} 

第二组图片:

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) { 

const char *sql = "SELECT id,cat_id,product_image FROM product where cat_id = '2'"; 


NSLog(@"sql is %s",sql); 

sqlite3_stmt *statement; 
// int catID = 0; 
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) { 
// We "step" through the results - once for each row. 
while (sqlite3_step(statement) == SQLITE_ROW) { 


tabName = [[NSString alloc] initWithUTF8String: 
(const char *) sqlite3_column_text(statement, 2)]; 
NSLog(@"tabName is %@",tabName); 

[tabArray addObject:tabName]; 
[tabName release]; 

// catID = sqlite3_column_int(statement, 0); 
} 
} 
sqlite3_finalize(statement); 
} 

else { 
sqlite3_close(database); 
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database)); 
// Additional error handling, as appropriate... 
}

}

我使用ActionSheet按钮单击显示数据数组:

-(void)actionSheetClickedButtonAtIndex:(int)buttonIndex {

     if (buttonIndex == 0) {
        NSLog(@"button");


 for (int i = 0; i<[mArray count]; i++ ) {
                NSLog(@"index %d",i);



              //  imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 500, 72, 72)];

                imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];

                Width = Width + 20+(i*74);

                [imgView1 setTag:i+1];

                [imgView1 addTarget:self action:@selector(arraysofsClicked:) forControlEvents:UIControlEventTouchUpInside];

                [imgView1 setImage:((Mysof *)[mArray objectAtIndex:i]).photo forState:UIControlStateNormal];

                [scrollview addSubview:imgView1];

              //  [myScroll addSubview:imgView1];



            }




    } else if (buttonIndex == 1) {

        NSLog(@"button 1");


 for (int i = 0; i<[tabArray count]; i++ ) {
                NSLog(@"index %d",i);



              //  imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 500, 72, 72)];

                imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];

                Width = Width + 20+(i*74);

                [imgView1 setTag:i+1];

                [imgView1 addTarget:self action:@selector(arraysofsClicked:) forControlEvents:UIControlEventTouchUpInside];

                [imgView1 setImage:((Mysof *)[tabArray  objectAtIndex:i]).photo forState:UIControlStateNormal];

                [scrollview addSubview:imgView1];

              //  [myScroll addSubview:imgView1];



            }


    } else if (buttonIndex == 2) {


          NSLog(@"button 2");

    } else if (buttonIndex == 3) {

    }
}

这里我不想使用cat_id = 1&amp; CAT_ID = 2。我需要自动传递任何增量参数值并从表中获取值。因为如果我改变了什么,cat_id就会增加。

1 个答案:

答案 0 :(得分:0)

您可以将cat_id的属性更改为不自动增量。此外,只有在向表中插入新行时才会触发自动增量。

似乎cat_id是描述产品类别的其他表的外键。只需使用UPDATE TABLE语法更改记录(例如使用新图像),保持cat_id不变。

如果用户更改了产品类别,则只需设置新的cat_id即可。

在SQLite中,用?sqlite_bind_*函数替换变量:

const *char sql = "SELECT id, cat_id, product_image FROM product "
                  "WHERE cat_id = ?";
...
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
   if (sqlite3_bind_int(sql, 1, yourCatID) == SQLITE_OK) {
       // iterate through the results with sqlite3_step
   }
}