简体   繁体   中英

Storing arraylist of arraylists in SQLite

I'm new to Android development, and I'm developing a memo/note-taking app that allows the user to include hashtags and then browse saved notes by said hashtags.

I already have a note app working that saves just the notes in an SQLite database, but I plan to store the hashtags by including an arraylist of hashtags within each of my note objects (which are already stored in an arraylist)...

My question is, how would I go about storing these arraylists of arraylists (note arraylists of hashtag arraylists) into an SQLite database?

Assuming you have a sql table for notes, you should create another table to store hashtags. You can link every hashtag to it's note by reserving a column to store the note id which hashtag belongs. Structure should be basically something like this:

Note table:

note_id | note

Hashtags table:

hashtag_id | hashtag | note_id

Here's one simple technique : Just store all your hashtags as a single string.

For example, say a note has 4 hashtags - #study, #homework, #school, #math

So, you just store them as a single string like:

String hastags="study#homework#school#math" 

Now store this string in sqlite databse as the hastag column for every note object.

Java allows you to easily parse strings, so when you retrieve a note, you can gets it's hashtag column and separate it into a string array (containing one string element for each hashtag) based on a delimiting character ("#" in this case) like so:

String hastags[]= hashtagStringFromDB.split("#");

Now, you have all your hashtags for that note object in a single string array which you can go through easily using hashtag[0], hashtag[1]...etc

Another option would be to create another hashtags table. Every hashtag goes into this table with a unique identifier (foreign key) to link it to a note in the notes table. So you can query this table with the note_id of a particular note and it will return all the hashtags belonging to that note.

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