【问题标题】:Data fetch from database从数据库中获取数据
【发布时间】:2013-08-29 05:21:31
【问题描述】:

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

【问题讨论】:

  • 您的问题充满了不相关的代码。请减少您的代码转储,并限制自己只显示绝对必要的内容。另外,请准确说明您的问题是什么。
  • 我正在使用 cat_id 检索数据。如果我编辑某些东西,cat_id 号码正在改变。因此,如果我使用 cat_id="%@" 这将获取所有值。我如何传递循环条件的参数..

标签: iphone sql database sqlite


【解决方案1】:

您可以将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
   }
}

【讨论】:

  • 这是我创建的表:CREATE TABLE "product" ("id" INTEGER PRIMARY KEY NOT NULL , "cat_id" INTEGER NOT NULL , "product_image" VARCHAR NOT NULL , "order_by" INTEGER NOT NULL )
  • 如果我设置 cat_id unchange 表示,它会显示 product_image 下的所有图像
  • 它不适用于您的代码。当我点击我得到它的按钮索引时,我得到了所有的 cat_id 并存储了单独的数组。
  • 如果您的数组不包含正确的项目,请更改它。该站点旨在解决特定的编程问题。您的问题是您不知道如何将变量替换为 SQLite sql 语句。我告诉你怎么做。如果您还有其他不相关的问题,请在接受上述答案后提出其他问题。
猜你喜欢
  • 2022-01-10
  • 1970-01-01
  • 2012-10-22
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多