在一个iPhone类中从一个SQLite数据库打开两个表?

时间:2010-10-25 17:22:58

标签: iphone sqlite

小澄清是否可以在一个iphone类中从一个sqlite db打开两个表???但我不能打开它请给我解决方案我是初学者

这里我尝试编码

  - (void)viewDidLoad {
      [super viewDidLoad];
     list = [[NSMutableArray alloc] init];
       if ([material isEqualToString:@"Derlin"]) {

        [self sel1];
    }
    else if([material isEqualToString:@"Helers"]){

     [self sel2];    
   }
  }
  -(void)sel1 {

  [self createEditableCopyOfDatabaseIfNeeded];
   NSLog(@"numberOfRowsInSection");
   sqlite3_stmt *statement = nil;  // create a statement
   const char *sql = "Select * from material";  //create a query to display in the tableView
    if(sqlite3_open([writableDBPath UTF8String], &database) == SQLITE_OK) 
    {
     NSLog(@"sqlite3_open");
     if(sqlite3_prepare_v2(database, sql,-1, &statement, NULL)!=SQLITE_OK) 
        NSAssert1(0,@"Error Preparing Statement",sqlite3_errcode(database)); 

    else
    {
    while(sqlite3_step(statement) == SQLITE_ROW) // if the connection exists return the row of the query table
       {
          achemical = [NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement,0)];
          arates = [NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement,1)];
          anotes = [NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement,2)];


          Chemical * chemic = [[Chemical alloc] initWithName:achemical rates:arates notes:anotes];

           [list addObject:chemic];
           [chemic release];
        }

      } 
   }
sqlite3_finalize(statement);

 }
  -(void)sel2 {
       [self createEditableCopyOfDatabaseIfNeeded];
      NSLog(@"numberOfRowsInSection"); 
       sqlite3_stmt *statement = nil;  // create a statement
      const char *sql = "Select * from material1";  //create a query to display in the tableView
      if(sqlite3_open([writableDBPath UTF8String], &database) == SQLITE_OK) 
     {
    NSLog(@"sqlite3_open");
    if(sqlite3_prepare_v2(database, sql,-1, &statement, NULL)!=SQLITE_OK) 
    NSAssert1(0,@"Error Preparing Statement",sqlite3_errcode(database)); 

    else
   {
          while(sqlite3_step(statement) == SQLITE_ROW) // if the connection exists return the row of the query table
      {
          achemical = [NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement,0)];
          arates = [NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement,1)];
          anotes = [NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement,2)];
           Chemical * chemic = [[Chemical alloc] initWithName:achemical rates:arates notes:anotes];

           [list addObject:chemic];
           [chemic release];

     }

    }
  }
  sqlite3_finalize(statement);

 }

提前致谢

1 个答案:

答案 0 :(得分:1)

当然,这很容易。下载FMDB,将其添加到您的项目中,并在您的类中添加两个FMResultSet个对象,每个对象一个(针对每个对象的查询)表。