litepal数据表关联

最近用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);
  1. 多对一
    歌曲和评论 多对一

歌曲持有评论的集合即可
- 增 见代码
- 删 删多的一方,一的一方自动修改
- 改 同上

    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();
  1. 多对多
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();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值