2012-04-23 75 views
0

我是这个数据库编程的新手。我从数据库中检索单列数据,但我无法从数据库检索多列数据。如何从数据库中检索多列数据?

这里是我的代码

-(void) readItemsFromDatabaseforTable:(NSString *)tableName { 


    arrayItems = [[NSMutableArray alloc] init]; 


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

      NSString *sql_str = [NSString stringWithFormat:@"select * from %@", tableName]; 

     const char *sqlStatement = (char *)[sql_str UTF8String]; 

      NSLog(@"query %s",sqlStatement); 

     sqlite3_stmt *compiledStatement; 

if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
{ 

      while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 

       NSString *idsstr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];    

       [arrayItems addObject:idsstr]; 
       NSLog(@"*************** %@",arrayItems); 
      } 
      while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
{ 

      NSString *caloriesstr = [NSString stringWithUTF8String: 

(char*)sqlite3_column_text(compiledStatement, 4)];    

       [caloriesarry addObject:caloriesstr]; 

       NSLog(@"naveenkumartesting"); 

       NSLog(@"*************** %@",caloriesarry); 

} 

} 

我的主要目的是从数据库中检索列的数据,并存储到多个阵列。

请你们谁能帮助我,

回答

1

使用此示例查询,并根据您的要求查询..

NSString *str = [NSString stringWithFormat:@"Select * from Yourtablename where image1 = '%@' AND person1 = '%@' AND category = '%@' AND PurchaseAmt = '%@'",Str,person1,category,Amountdata]; 

Use this tutorial

+0

在我的程序中,我能够检索单列数据,但锄头检索多列数据。我尝试了上述方法但没有use.please给任何其他解决方案... – kumar 2012-04-23 05:08:09

2

你不需要循环与while第二次当你可以在一次获得所有数据的同时如此:

const char *stmt = "select id, name from table1 where isImage = 1"; 
sqlite3_stmt *selectstmt; 
if (sqlite3_prepare_v2(database, stmt, -1, &selectstmt, NULL) == SQLITE_OK) { 
    while(sqlite3_step(selectstmt) == SQLITE_ROW) { 
     int rowid = sqlite3_column_int(selectstmt, 0); 
     NSString * name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]; 

    } 
}