• 数据库接口使用示例


    考虑到我们的接口文档可能不是很清晰,昨天对接的时候也发现UI人员对我们的接口逻辑不太理解,所以这里写几个典型范例。虽然对接也快对接完了(逃,但是这样至少免得以后忘记了。

    (注:这里的代码都测试过,放在HeartTraceappsrcandroidTestjavacomexampledelldbexamples中。持续性更新。)


    几个基本原则

    任何新建的对象,在insert之前,是不会到数据库里的!!!!!!不管怎样都不会!!!!!!!!!

    任何已有的对象,在修改之后,如果不update,修改也不会被更新到数据库里!!!!!!!!!不管怎样都不会!!!!!!!!!!

    只有通过getAll, getByRestrict, getAllLabel等方法获取的Diary Sentence Label Diarybook Sentencebook对象才指向数据库中的一个表项,直接new的会被认为是一个新的表项!!!

    注:

    Diary要求:text(日记内容)可以为空,date(日期)不能为空,diary book(日记本)可以为空(TODO:这个以后肯定会改成不能为空的!)

    Sentence要求:text(日记内容)可以为空,date(日期)不能为空,sentence book(瓶子)不能为空

    Diarybook要求:name(名字)不能为空,且彼此不能重复

    Sentencebook要求:name(名字)不能为空,且彼此不能重复


    获取、释放DatabaseHelper

    方法1:

            // 获取
            DatabaseHelper helper = new DatabaseHelper(context); // 这个context视情况而定,可能是this或者getApplicationContext()
    
            // 释放
            helper.close();
    

    方法2:

            // 获取
            DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); // context同上
    
            // 释放
            OpenHelperManager.releaseHelper();
    

    目录

    Example 0. 将一个新的日记插入已有日记本,并插入标签
    Example 1. 通过名字判断某个瓶子是否存在;如果不存在,则新建这个瓶子。并将新建的小纸条插入这个瓶子
    Example 2. 通过单个Label查找所有的Diary
    一些常见的错误操作


    Example 0. 将一个新的日记插入已有日记本,并插入标签

    前提:diarybook是一个已经插入数据库的日记本,helper是已有的DatabaseHelper

            Diary diary = new Diary("hiasjfhlaf");
            diary.setDate();
            diary.setDiarybook(diarybook);
            diary.insert(helper);
    
            Label label1 = new Label("label1");
            label1.insert(helper);
    
            Label label2 = new Label("label2");
            label2.insert(helper);
    
            diary.insertLabel(helper, label1);
            diary.insertLabel(helper, label2);
    
            List<Label> list = diary.getAllLabel(helper);
            for(Label l : list) {
                Log.d(getClass().getName(), "testExample0: label " + l.getLabelname());
            }
    

    如果想要对已有的diary进行insertLabel操作,其实完全同理。


    Example 1. 通过名字判断某个瓶子是否存在;如果不存在,则新建这个瓶子。并将新建的小纸条插入这个瓶子

    前提:helper是已有的DatabaseHelper,bookName是已有的String。

            Sentencebook b = Sentencebook.getByName(helper, bookName);
            if(b == null) {
                b = new Sentencebook(bookName);
                b.insert(helper);
            }
    
            Sentence sentence = new Sentence("adfjanvnavop");
            sentence.setDate();
            sentence.setSentencebook(b);
            sentence.insert(helper);
    

    如果想要通过名字获得某个Label,假如不存在就新建Label,代码同理。


    Example 2. 通过单个Label查找所有的Diary

    前提:helper是已有的DatabaseHelper,label已经在数据库内(这个很重要,不然会报错的)。

            List<Label> labelList = new ArrayList();
            labelList.add(label);
    
            List<Diary> d = Diary.getByRestrict(helper, null, null, null, labelList, true);
    


    一些常见的错误操作

    对于Label Diarybook Sentencebook这三个要求名字不能重复的类,如果重复插入名字相同的对象肯定会崩溃的。


    另 关于出了问题怎么办

    如果是和数据库有关的错误,请立刻联系数据库人员,不要试图自己瞎jb调!

    把一小段代码发过来有时候是有用的,但有的时候是没用的。比较好的方法是把整个工程文件弄过来,然后告诉数据库人员如何复现情景,因为数据库人员 真 的 不知道你们点哪个键怎么跳转,怎么才能触发错误……

    当然最好的方法是带着电脑,跟数据库的人面♂调

    问:如果现在有一个错误,但是三个写数据库的人都不理我怎么办?

    答:买个西瓜/冰淇淋,到空调房里看电影或写作业,直到写数据库的人回复

    或者,如果水群在线只是不想理你的话,可以提出给水群买个奶茶w

  • 相关阅读:
    java课后思考问题(二)
    论文-MS-CNN
    论文-ION--Inside-Outside Net: Detecting Objects in Context with Skip
    51Nod--1285-山峰和分段
    论文-Learning Features and Parts for Fine-Grained Recognition
    论文-Multi-view Convolutional Neural Networks for 3D Shape Recognition
    论文-SPP_net
    Leetcode 448. Find All Numbers Disappeared in an Array
    FlowNet
    LeetCode Weekly Contest 21
  • 原文地址:https://www.cnblogs.com/USTC-CC/p/9154712.html
Copyright © 2020-2023  润新知