I am quite unskilled with using databases in programs, and so i need a little of guidance.
I've got a Java object with several primitive fields, and many references to Strings and ArrayList objects. The primitives and Strings map nicely to available SQLite fields, but I am unsure the way i can persist the ArrayLists.
I had been entertaining two ideas, such as to serialise the ArrayLists and store these questions Text area, another is to possess a column which indicates a table with arity 1, by which I'm able to keep individual strings, but I am unclear about how you can implement this in android. I am available to different approaches, however i wouldn't understand how to implement the second in java using SQLite, so an answer could be lovely. Thanks.
Generally (from things i have discovered) for those who have an item, which itself consists of a listing of other objects, that might be single to a lot of (or potentially many-to-many) relationship. To keep this data you would like to use another table. Within the other table, you'll have most of your key for that object, after which an overseas key referencing parents resist so it goes. See this link for any better explanation.
CREATE TABLE User ( _id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT ); CREATE TABLE UserPicture( _id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, userId INTEGER, path TEXT FOREIGN KEY(userId) REFERENCES User(_id) );
Now if you have a person object, with a listing of UserPictures', whenever you save towards the database you may wish to iterate over each picture and place each one of these in to the UserPicture table, while using userId because the link to the consumer table.
Within the demonstration of a many-to-many relationship, each Object might have a listing of the children objects. A much better illustration of this is a subscriptionOrPart system. A Person might have a listing of Roles, along with a Role might have a listing of Customers, since a person can (normally) maintain multiple roles, along with a role can obviously have multiple customers. This could simply require what is known as a join table It is. UserInRole might have two posts, UserID and RoleID to exhibit that User X goes to Role Y.
Not understanding further particulars, I'm able to state that the textbook method of doing this really is to produce a second table for that array list, after which range from the id from the primary record within the array list.
For instance, suppose your object includes
String name; int age; ArrayList<String> hobbies;
You'd create tables such as this:
create table person (personid int, name varchar(30), age int); create table hobby (hobbyid int, personid int, description varchar(30));
Your data might seem like this:
Person 11 Bob 18 12 Sally 68 13 Ford 42 Hobby 21 11 fishing 22 11 hunting 23 12 needlepoint 24 12 rock-climbing 25 13 hitch-hiking
To obtain the listing of anyone's hobbies, you'd make use of a query like:
select person.name, hobby.description from person join hobby on person.personid=hobby.personid