简体   繁体   中英

Storing an ArrayList of Arrays in SQLite Database

My question has to do with storing arrays and ArrayLists in SQLite. 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:

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. I want to store a list of Courses in a SQLite database. 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.)

Create several tables.

One table ('Cource') contains field 'name' and field with reference('id' field) to row in table 'Tee'.

Table 'Tee' contains fields 'id', 'name', 'slop', 'rating', 'par' and '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(); for(int i = 0; i < array.length - 1; i++) { if(i != 0) { b.append(","); } b.append(array[i]); } String 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(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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