litepal資料表關聯
阿新 • • 發佈:2019-01-07
最近用litepal操作資料很方便,記錄下表關聯
1. 一對一
song和introduction 即音樂和簡介 Song中持有introduction 的引用,introduction 持有Song的引用,就建立了一對一關係,
- 增 如下面程式碼所示
- 刪 刪除song一行,對應的introduction 自動刪除,反之亦然。
- 改 修改introduction 某行,對應的song自動被修改
Introduction introduction = new Introduction();
introduction.setTitle("這是song1111 介紹");
Song song1 = new Song();
song1.setTitle("song111");
song1.setIntroduction(introduction);
introduction.save();
song1.save();
Song songData = DataSupport.findLast(Song.class, true);
Introduction introductionData = DataSupport.findLast(Introduction.class, true);
Log.e(TAG, "onCreate:songData " + songData);
Log.e(TAG, "onCreate:introductionData " + introductionData);
- 多對一
歌曲和評論 多對一
歌曲持有評論的集合即可
- 增 見程式碼
- 刪 刪多的一方,一的一方自動修改
- 改 同上
Song song1 = new Song();
song1.setTitle("song111");
Commment comment1 = new Commment();
comment1.setTitle("111");
Commment comment2 = new Commment();
comment2.setTitle("222" );
song1.getCommentList().add(comment1);
song1.getCommentList().add(comment2);
comment1.save();
song1.save();
- 多對多
Song song1 = new Song();
song1.setTitle("song111");
song1.save();
Song song2 = new Song();
song2.setTitle("song222");
song2.save();
Category category1 = new Category();
category1.setTitle("111");
category1.getSongArrayList().add(song1);
category1.getSongArrayList().add(song2);
category1.save();
Category category2 = new Category();
category2.setTitle("222");
category2.getSongArrayList().add(song1);
category2.getSongArrayList().add(song2);
category2.save();