我在我的服务器中存储了一些信息。我使用 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 & cat_id=2。我需要自动传递任何参数值以进行增量并从表中获取值。因为如果我改变任何东西,cat_id 就会增加。