Reputation: 101
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.)
Upvotes: 2
Views: 3074
Reputation: 351
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:
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();
Upvotes: 1
Reputation: 121
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
Upvotes: 0