Database Interface Usage Example

Source: Internet
Author: User

Considering that our interface documentation may not be very clear, we found yesterday that the UI staff didn't understand our interface logic very well, so here are some typical examples. Although the docking is almost finished (escape, but so at least lest later forget.)

(Note: The code here has been tested and put in HeartTrace\app\src\androidTest\java\com\example\dell\db\examples\ .) Persistent updates. )


Several basic principles

Any new object, before insert, is NOT!!!!!! in the database. It's not!!!!!!!!! anyway.

Any existing objects, after the modification, if not update, the changes will not be updated to the database!!!!!!!!! It's not!!!!!!!!!! anyway.

Only the diary sentence Label Diarybook Sentencebook object obtained by means of GetAll, getbyrestrict, Getalllabel, etc., points to a table entry in the database. The direct new will be considered a new table item!!!

Note:

Diary requirements: Text (journal content) can be empty, date (date) cannot be empty, Diary Book (journal) can be empty (TODO: This will definitely change to not empty!) )

Diary requirements: Text (journal content) can be empty, date (date) cannot be empty, sentence Book (journal) cannot be empty

Diarybook Requirements: Name (name) cannot be empty and cannot be duplicated with each other

Sentencebook Requirements: Name (name) cannot be empty and cannot be duplicated with each other


Get, Release Databasehelper Method 1:
        // 获取        newDatabaseHelper// 这个context视情况而定,可能是this或者getApplicationContext()        // 释放        helper.close();
Method 2:
        // 获取        DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class// context同上        // 释放        OpenHelperManager.releaseHelper();


Directory

Example 0. Insert a new journal into an existing journal and insert a label
Example 1. Determine if a bottle exists by name, or create a new one if it does not exist. and insert the new little note into the bottle.
Example 2. Find all diary with a single label



Example 0. Insert a new journal into an existing journal and insert a label

Premise: Diarybook is a journal that has been inserted into a database, helper is an existing Databasehelper

Diary Diary =New Diary("Hiasjfhlaf"); Diary.setDate(); Diary.Setdiarybook(Diarybook); Diary.Insert(helper); Label Label1 =NewLabel ("Label1"); Label Label1.Insert(helper); Label2 =NewLabel ("Label2"); Label2.Insert(helper); Diary.Insertlabel(helper, Label1); Diary.Insertlabel(helper, Label2); List<label> list = Diary.Getalllabel(helper);assertequals(2, list.size()); for(Label l:list) {Log.D(GetClass().GetName(),"Testexample0:label"+ L.Getlabelname()); }

If you want to insertlabel an existing diary, it's exactly the same.



Example 1. Determine if a bottle exists by name, or create a new one if it does not exist. and insert the new little note into the bottle.

Prerequisite: Helper is an existing databasehelper,bookname is an existing string.

        Sentencebook b = Sentencebook.getByName(helper, bookName);        ifnull) {            newSentencebook(bookName);            b.insert(helper);        }        newSentence("adfjanvnavop");        sentence.setDate();        sentence.setSentencebook(b);        sentence.insert(helper);




Example 2. Find all diary with a single label

Prerequisite: The helper is an existing Databasehelper,label already in the database (this is very important, otherwise will be error).

        new ArrayList();        labelList.add(label);        List<Diary> d = Diary.getByRestrictnullnullnulltrue);


And what about the problem?

If it is a database-related error, please contact the database personnel immediately, do not attempt to blind JB tune!

It's sometimes useful to send a small piece of code, but it's useless at times. The better way is to get the entire project file, and then tell the database staff how to reproduce the situation, because the database staff really do not know which key you click How to jump, how to trigger the error ...

Of course, the best way is to take the computer, with the database of human face ♂ Tune

Q: If there is an error now, but three people who write the database ignore me what to do?

A: Buy a watermelon/ice cream, go to the air-conditioned room to see the film or the writing industry, until the person who wrote the database replied

Or, if the water group online just don't want to talk to you, you can ask water group buy a milk tea W

Database Interface Usage Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.