0

我在我的服务器中存储了一些信息。我使用 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 就会增加。

4

1 回答 1

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
   }
}
于 2013-08-27T08:43:42.380 回答