【问题标题】:how to retrieve the multiple columns data from the database?如何从数据库中检索多列数据?
【发布时间】:2012-05-03 17:36:32
【问题描述】:

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

这是我的代码

-(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);

}

}

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

请任何人帮助我,

【问题讨论】:

    标签: iphone objective-c ios sqlite nsmutablearray


    【解决方案1】:

    当您可以在一个 while 中获取所有数据时,您不需要第二次循环使用 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)];
    
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      Use this tutorial

      【讨论】:

      • 在我的程序中,我可以检索单列数据,但要检索多列数据。我尝试了上述方法,但没有用。请提供任何其他解决方案...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      • 2013-10-31
      • 2016-11-27
      • 2016-03-17
      • 2015-12-03
      • 1970-01-01
      相关资源
      最近更新 更多