2013-08-26 12 views
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'"; 같은 다른 이미지 집합에 대해 다른 Array를 사용하고 있습니다. "% @"를 사용하면 모든 값이 표시됩니다.데이터베이스에서 데이터 가져 오기

번호 : 화상

먼저 설정 : 이미지

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... 
} 

}

임 datas의 디스플레이 배열 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를 사용하고 싶지 않습니다. 나는 increament에 대한 매개 변수 값을 자동으로 전달하고 테이블에서 값을 가져와야합니다. 왜냐하면 내가 아무것도 바꾸면 cat_id가 증가하기 때문입니다.

+0

귀하의 질문은 관련이없는 코드로 어수선하다. 코드 덤프를 줄이고 절대적으로 필요한 것만 표시하도록하십시오. 또한 문제가 무엇인지 정확하게 기술하십시오. – Mundi

+0

메신저 데이터를 검색하는 데 cat_id를 사용합니다. 뭔가 편집하면 cat_id 번호가 바뀝니다. 그래서 cat_id = "% @"를 사용하면 모든 값을 얻게됩니다. 어떻게 루프 조건에 대한 매개 변수를 전달할 수 있습니다 .. – user2674668

답변

0

cat_id의 속성을 자동 증가가 아닌 것으로 변경할 수 있습니다. 또한 자동 증가는 테이블에 새 행을 삽입 할 때만 트리거되어야합니다.

cat_id은 제품 카테고리를 설명하는 다른 테이블의 외래 키입니다. 을 변경하지 않고 (예 : 새 이미지로) 레코드를 변경하려면 UPDATE TABLE 구문을 사용하기 만하면됩니다.

사용자가 제품 범주를 변경하면 새 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 
    } 
} 
+0

이것은 내가 만든 테이블입니다 : CREATE TABLE "product"("id"INTEGER PRIMARY KEY NOT NULL, "cat_id"INTEGER NOT NULL, "product_image"VARCHAR NOT NULL, "order_by" INTEGER NOT NULL) – user2674668

+0

그래서? 대체가 효과가 있습니까? – Mundi

+0

cat_id가 변경되지 않는다는 것을 의미하면 product_image 아래에 모든 이미지가 표시됩니다. – user2674668

관련 문제