简体   繁体   English

在SQLite数据库中存储数组的ArrayList

[英]Storing an ArrayList of Arrays in SQLite Database

My question has to do with storing arrays and ArrayLists in SQLite. 我的问题与在SQLite中存储数组和ArrayList有关。 I have an Object Course as follows: 我有一个对象课程,如下所示:

public class Course {
    private String name;
    private ArrayList<Tee> tees;

which contains the ArrayList of Tee where Tee looks like this: 其中包含Tee的ArrayList,其中Tee如下所示:

public class Tee {
    private String Name;
    private int Slope;
    private double Rating;
    private int[] Par={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    private int[] HCP={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

Each Course has a number of Tees associated with it and each Tee contains 2 integer arrays. 每个课程都有许多与之关联的Tees,每个Tee包含2个整数数组。 I want to store a list of Courses in a SQLite database. 我想在SQLite数据库中存储课程列表。 Each row of the database will represent a Course and contain the Course object data. 数据库的每一行将代表一个课程,并包含课程对象数据。 I have researched this and I find either too little or too much information to be helpful. 我对此进行了研究,发现信息太少或太少而无济于事。

I think I need to use a JSON object or a BLOB to convert the ArrayLists to something that can be stored in SQLite, but I can't seem to find the way to convert an int array into such a thing (BLOB or JSON Object or JSON Array?) and then the subsequent list of Tees into another thing (BLOB, etc.) 我想我需要使用JSON对象或BLOB将ArrayLists转换为可以存储在SQLite中的对象,但是我似乎找不到找到将int数组转换为此类对象的方法(BLOB或JSON Object或JSON Array?),然后将随后的Tees列表转换为另一件事(BLOB等)

Create several tables. 创建几个表。

One table ('Cource') contains field 'name' and field with reference('id' field) to row in table 'Tee'. 一个表(“ Cource”)包含字段“ name”和带有引用(“ id”字段)的字段,以引用表“ Tee”中的行。

Table 'Tee' contains fields 'id', 'name', 'slop', 'rating', 'par' and 'hcp'. 表“ Tee”包含字段“ id”,“名称”,“ slop”,“ rating”,“ par”和“ hcp”。

Since it is impossible to store an array in a table row you can: 由于不可能将数组存储在表行中,因此您可以:

  • convert array to string 将数组转换为字符串
  • create a field for each item in array 为数组中的每个项目创建一个字段

    StringBuilder b = new StringBuilder(); StringBuilder b =新的StringBuilder(); for(int i = 0; i < array.length - 1; i++) { if(i != 0) { b.append(","); for(int i = 0; i <array.length-1; i ++){if(i!= 0){b.append(“,”); } b.append(array[i]); } b.append(array [i]); } String stringArr = b.toString(); }字符串stringArr = b.toString();

You have to create the framework for retrieving data. 您必须创建用于检索数据的框架。

Each course you should treat like one object. 您应该将每门课程都视为一个对象。

  public class Tee {

    private String Name;
    private int Slope;

    private double Rating;
    private int[] Par={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    private int[] HCP={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

    public Tee(String name,int slope,double Rating,int[] par,int[] hcp)
    {

     this.name=name;
     this.slope=slope;
     this.Rating=rating;
     this.PAR=par;
     this.HCP=hcp;
   }

public String GetName()
{
return this.name;
}
....

public int[] GetHCP()
{
return this.HCP;
}
}

And Use for loop for inserting data into the database 并使用for循环将数据插入数据库

for(int i=0;i<size();i++)
    {
        ContentValues values=new ContentValues();

        values.put("name", "data");



        long l=sdb.insert(TAble name, null, values);



    }

For PAR and HCP data storing, please do the table indexing for good way manage your data flow 对于PAR和HCP数据存储,请进行表索引编制,以便更好地管理数据流

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM