在SQLite中查询时,是否可以将值从列拆分为行?

时间:2016-09-16 08:39:39

标签: android sqlite

从下面的表A中,我想从列Color和中拆分值 用它生成新行。我想要的结果显示在表结果中 下方。

表A

Animal | Gender | Color
-------|--------|--------------------- 
 Cat   |  Male  | White, Black, Brown
 Cat   | Female | White 
 Dog   |  Male  | Black, Yellow 
 Dog   | Female | Black 

表格结果

Animal | Gender | Color
-------|--------|--------------------- 
 Cat   |  Male  | White 
 Cat   |  Male  | Black 
 Cat   |  Male  | Brown 
 Cat   | Female | White 
 Dog   |  Male  | Black 
 Dog   |  Male  | Yellow 
 Dog   | Female | Black

我如何使用SQLite实现这一目标,还是有任何其他建议?

1 个答案:

答案 0 :(得分:2)

假设您正在将每条记录作为字符串获取,如下所述,您可以从中分割所有数据,如下所示:

创建一个包含动物,颜色和性别等属性的类,如下所示

  class AnimalData         // You can give any name 
  {
      String strAnimal, strGender, strColor;

      public AnimalData(String strAnimal, String strGender, String strColor)
      {
          this.strAnimal = strAnimal;
          this.strGender = strGender;
          this.strColor = strColor;
      }

      public String getStrAnimal()
      {
          return strAnimal;
      }

      public String getStrGender()
      {
          return strGender;
      }

      public String getStrColor()
      {
          return strColor;
      }
  }

现在我正在使用3个静态变量,其中包含您从数据库获得的数据,您需要使用它们而不是以下变量

    String strAnimalData[] = {"Cat","Cat","Dog","Dog"};
    String strGenderData[] = {"Male","Female","Male","Female"};
    String strColorData[] = {"White,Black,Brown","White","Black,Yellow","Black"};

现在我正在使用一种方法,它会从上面的值中给出结果列表

public List<AnimalData> getAllAnimalData()
{
      List<AnimalData> strAllAnimalData = new ArrayList<>();

      for(int i = 0; i < strAnimalData.length; i++)
      {
          if(strAnimalData[i].indexOf(",") > 0)
          {
              String strSubAnimalData[] = strAnimalData[i].split(",");

              for(int j = 0; j < strSubAnimalData.length; j++)
              {
                  AnimalData ad = new AnimalData(strSubAnimalData[j], strGenderData[i], strColorData[i]);
                  strAllAnimalData.add(ad);
              }
          }
          else if(strGenderData[i].indexOf(",") > 0)
          {
              String strSubGenderData[] = strGenderData[i].split(",");

              for(int j = 0; j < strSubGenderData.length; j++)
              {
                  AnimalData ad = new AnimalData(strAnimalData[i], strSubGenderData[j], strColorData[i]);
                  strAllAnimalData.add(ad);
              }
          }
          else if(strColorData[i].indexOf(",") > 0)
          {
              String strSubColorData[] = strColorData[i].split(",");

              for(int j = 0; j < strSubColorData.length; j++)
              {
                  AnimalData ad = new AnimalData(strAnimalData[i], strGenderData[i], strSubColorData[j]);
                  strAllAnimalData.add(ad);
              }
          }
          else
          {
              AnimalData ad = new AnimalData(strAnimalData[i], strGenderData[i], strColorData[i]);
            strAllAnimalData.add(ad);
          }
      }

      // Below code is just for Display Data

      for(AnimalData tempData: strAllAnimalData)
      {
          System.out.println("\t" + tempData.getStrAnimal() + "\t" + tempData.getStrGender() + "\t" + tempData.getStrColor());
        System.out.println("\n");
    }
}

注意: - 目前,当每个位置只有一个变量有更多数据时,这将起作用,例如,如果动物在位置1处有“Cat,Dog”而Color在位置上也有“红色,棕色” 1然后它只会将动物与颜色分开,因为你需要在循环中实现相同的逻辑。