ContentProvider and Database use

Source: Internet
Author: User
Tags static class

Package Practise.lxm.myprovider;import android.net.uri;import android.provider.basecolumns;/** * Created by Administrator on 2015/4/9. * Tool class, provided to call the ContentProvider Contentresolver */public class Words {public    static final String authority = "Lxm.practi Se.provide ";    Word table column name class public    static class Word implements basecolumns{        //Allow operation of column public        static final String _id = "_id"; 
   public static final String word = "word";        public static final String DETAIL = "DETAIL";        The two URI public        static final URI provided by All_words = Uri.parse ("content://" + Authority + "/words");        public static final Uri One_word = Uri.parse ("content://" + Authority + "/word");}    }


Package Practise.lxm.myprovider;import Android.content.contentprovider;import Android.content.contenturis;import Android.content.contentvalues;import Android.content.urimatcher;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import android.net.uri;/** * Created by Administrator on 2015/4/9.    */public class MyProvider extends ContentProvider {public static final String table_name= "words";    Operation database Private Mysqliteopendatabase dbhelper;    Urimatcher private static Urimatcher Urimatcher = new Urimatcher (urimatcher.no_match);    Registration code private static final int all_words_code = 1;    private static final int words_id_code = 2;        static {Urimatcher.adduri (words.authority, "Words", All_words_code);    Urimatcher.adduri (words.authority, "word/#", Words_id_code); } @Override public Boolean onCreate () {dbhelper = new Mysqliteopendatabase (This.getcontext (), "myprovider.db3        ", 1);    return true;  }//Query @Override  Public Cursor query (URI uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) {S Qlitedatabase db = Dbhelper.getreadabledatabase (); Get database Operations Object switch (Urimatcher.match (URI)) {//query all case All_words_code:return Db.query (T            Able_name,projection,selection,selectionargs,null,null,sortorder);                Case Words_id_code://Query long id= Contenturis.parseid (URI) by ID;                String Wordswhere = words.word._id + "=" + ID;                if (selection! = null && selection.length () > 0) {wordswhere + = "+" + selection;            } return Db.query (Table_name,projection,wordswhere,selectionargs,null,null,sortorder);        Default://Unknown throw new IllegalArgumentException ("Unknown URI:" + uri.tostring ()); }}//Get type @Override public String getType (URI uri) {switch (Urimatcher.match (URI)) {case A Ll_worDs_code://Multiple return "Vnd.android.cursor.dir/practise.lxm.myprovider.word";            Case Words_id_code://Single return "Vnd.android.cursor.item/practise.lxm.myprovider.word";        Default://Unknown throw new IllegalArgumentException ("Unknown URI:" + uri.tostring ()); }}//New @Override public URI insert (URI uri, contentvalues values) {sqlitedatabase db = DBHELPER.GETW Ritabledatabase ();//Get Operation Database object switch (Urimatcher.match (URI)) {case All_words_code://Add to Number                According to the library long row_id = Db.insert (table_name,null,values);                    if (row_id > 0) {//new success URI Newuri = Contenturis.withappendedid (URI, row_id);                    Notification data has changed GetContext (). Getcontentresolver (). Notifychange (Newuri,null);                return Newuri;            } break; Default://Unknown throw new IllegalArgumentException ("Unknown URI: "+ uri.tostring () +" code: "+ Urimatcher.match (URI));    } return null; } @Override public int delete (URI Uri, String selection, string[] selectionargs) {Sqlitedatabase db = Dbhelp Er.getreadabledatabase ();//Gets the Operation database object int delcount = 0;                Number of deletions switch (Urimatcher.match (URI)) {case Words_id_code://single//Add delete condition, ID for specified value                Long delid = Contenturis.parseid (URI);                String Delwhere = words.word._id + "=" + delid;                if (selection! = null && selection.length () > 0) {delwhere + = "+" + selection;                }//Execute delete delcount = Db.delete (Table_name,delwhere,selectionargs);            Break            Case All_words_code://Delete Multiple Delcount = Db.delete (Table_name,selection,selectionargs);        Default://Unknown throw new IllegalArgumentException ("Unknown URI:" + uri.tostring ());      }  Notification data has changed GetContext (). Getcontentresolver (). Notifychange (Uri,null);    return delcount; } @Override public int update (URI uri, contentvalues values, String selection, string[] selectionargs) {SQLi Tedatabase db = Dbhelper.getwritabledatabase ();//Gets Operation database object int updatecount = 0;                Number of updates switch (Urimatcher.match (URI)) {case Words_id_code://single//Add modify condition, ID is specified value                Long UpdateID = Contenturis.parseid (URI);                String Updatewhere = words.word._id + "=" + UpdateID;                if (selection! = null && selection.length () > 0) {updatewhere + = "+" + selection;                }//Execute delete updatecount = Db.update (table_name,values, Updatewhere,selectionargs);            Break            Case All_words_code://Delete Multiple Updatecount = Db.update (table_name,values, Selection,selectionargs); Default://Unknown throw nEW illegalargumentexception ("Unknown URI:" + uri.tostring ());        }//Notification data changed GetContext (). Getcontentresolver (). Notifychange (Uri,null);    return updatecount; }}


ContentProvider and Database use

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.