简体   繁体   English

在SQLite中存储数组列表的数组列表

[英]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. 我是Android开发的新手,并且正在开发一个备忘录/笔记应用程序,该应用程序允许用户包括主题标签,然后通过所述主题标签浏览保存的笔记。

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)... 我已经有一个便笺应用程序可以在SQLite数据库中仅保存便笺,但是我计划通过在我的每个便笺对象中包含一个哈希标签数组列表(已经存储在一个数组列表中)来存储哈希标签...

My question is, how would I go about storing these arraylists of arraylists (note arraylists of hashtag arraylists) into an SQLite database? 我的问题是,我将如何将这些数组列表的数组列表(注意标签数组列表的数组列表)存储到SQLite数据库中?

Assuming you have a sql table for notes, you should create another table to store hashtags. 假设您有一个用于注释的sql表,则应创建另一个表来存储主题标签。 You can link every hashtag to it's note by reserving a column to store the note id which hashtag belongs. 您可以通过保留一列存储该标签所属的注释ID的方式,将每个标签都链接到其注释。 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 例如,假设某条笔记有4个标签- #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. 现在,将此字符串存储在sqlite数据库中,作为每个注释对象的hastag列。

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: Java使您可以轻松地分析字符串,因此,在检索注释时,您可以获取它的hashtag列,并将其基于定界字符(在这种情况下为“#”)分成一个字符串数组(每个主题标签包含一个字符串元素)像这样:

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 现在,您可以在单个字符串数组中拥有该笔记对象的所有标签,可以轻松使用hashtag[0], hashtag[1]...etc

Another option would be to create another hashtags table. 另一个选择是创建另一个hashtags表。 Every hashtag goes into this table with a unique identifier (foreign key) to link it to a note in the notes table. 每个主题标签都带有唯一的标识符(外键)进入该表,以将其链接到notes表中的notes 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. 因此,您可以使用特定注释的note_id查询此表,它将返回属于该注释的所有主题标签。

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

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