Android content provider ContentProvider implementation of database additions and deletions

Source: Internet
Author: User

Tagged with: Android database, SQLite JUnit for Android

1. We first new one our own class integration contentprovider, and the implementation method is as follows

Package Com.wzw.sqllitedemo.providers;import Com.wzw.sqllitedemo.db.personsqliteopenhelper;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;public class Personcontentprovider extends ContentProvider {private static, Urimatcher Urimatcher; private static final String authority= "Com.wzw.sqllitedemo.providers.PersonContentProvider";p rivate static final int Person_insert_code = 0;private static final int person_delete_code = 1;private static final int person_update_code = 2;pri vate static final int person_queryall_code = 3;private Personsqliteopenhelper mopenhelper;static{urimatcher=new Urimatcher (Urimatcher.no_match);//Add URI (extension number)//content://com.wzw.sqllitedemo.providers.personcontentprovider/ Person/inserturimatcher.adduri (Authority, "Person/insert", Person_insert_code); Urimatcher.adduri (Authority,"Person/delete", Person_delete_code); Urimatcher.adduri (Authority, "Person/update", Person_update_code); Urimatcher.adduri (Authority, "Person/queryall", Person_queryall_code);} @Overridepublic Boolean onCreate () {mopenhelper=new personsqliteopenhelper (GetContext ()); return false;} @Overridepublic Cursor query (Uri uri, string[] projection, string selection,string[] Selectionargs, string sortOrder) {SW Itch (Urimatcher.match (URI)) {case Person_queryall_code:sqlitedatabase db=mopenhelper.getreadabledatabase (); Db.isopen ()) {cursor cursor = db.query ("person", projection, selection, Selectionargs, NULL, NULL, sortOrder); return Cursor;//db.close (); Cannot close database}break;default:throw new IllegalArgumentException ("Uri mismatch") when returning cursor result set;} return null;} @Overridepublic String getType (Uri uri) {//TODO auto-generated method Stubreturn null;} @Overridepublic uri insert (URI uri, contentvalues values) {switch (Urimatcher.match (URI)) {case person_insert_code:// Add people to Person table sqlitedatabase db = Mopenhelper.getwritabledaTabase (); if (Db.isopen ()) {Long id = db.insert ("person", null, Values);d b.close ();//return type is content:// Com.wzw.sqllitedemo.providers.personcontentprovider/person/insert/idreturn Contenturis.withappendedid (URI, id);} Break;default:throw new IllegalArgumentException ("Uri mismatch");} return null;} @Overridepublic int Delete (URI Uri, String selection, string[] selectionargs) {switch (Urimatcher.match (URI)) {case Perso n_delete_code://Delete data in person sqlitedatabase db = Mopenhelper.getwritabledatabase (); if (Db.isopen ()) {int count= Db.delete ("Person", selection, Selectionargs);d b.close (); return count;} Break;default:throw new IllegalArgumentException ("Uri mismatch" +uri);} return 0;} @Overridepublic int update (URI uri, contentvalues values, String selection,string[] selectionargs) {switch ( Urimatcher.match (URI)) {Case person_update_code://Update person table Sqlitedatabase db = Mopenhelper.getwritabledatabase (); if (Db.isopen ()) {int count=db.update ("person", values, Selection, Selectionargs);d b.close (); return count;} Break;default:tHrow new IllegalArgumentException ("Uri mismatch" +uri);} return 0;}}

Declaring in the manifest file

        <!--declared in the manifest file Contextprovider--        <provider             android:name= " Com.wzw.sqllitedemo.providers.PersonContentProvider "            android:authorities=" Com.wzw.sqllitedemo.providers.PersonContentProvider ">                    </provider>

2. Then we're going to create a new project to test

Create a new JUnit test class


Package Com.wzw.other;import Android.content.contentresolver;import Android.content.contenturis;import Android.content.contentvalues;import Android.database.cursor;import Android.net.uri;import Android.test.androidtestcase;import Android.util.log;public class TestCase extends Androidtestcase {private String tag = "TestCase";p ublic void Testinsert () {//Uriuri URI of another program = Uri.parse ("content:// Com.wzw.sqllitedemo.providers.personcontentprovider/person/insert ");//Gets the content provider Access Object Contentresolver resolver = GetContext (). Getcontentresolver (); Contentvalues values = new Contentvalues () values.put ("name", "Meimei"); Values.put ("Age", 18); LOG.I (tag, "uri" +uri); uri = Resolver.insert (URI, values); Long id = Contenturis.parseid (URI); LOG.I (tag, "inserted ID" +id);} public void Testdelete () {//Uriuri URI for another program = Uri.parse ("content:// Com.wzw.sqllitedemo.providers.personcontentprovider/person/delete ");//Gets the content provider Access Object Contentresolver resolver = GetContext (). Getcontentresolver (); String where = "_id=?"; String[] selectionargs={"8"};int count = Resolver.delete (URI, where, Selectionargs); LOG.I (Tag, "deleted" +count); public void Tesupdate () {//Uriuri URI for another program = Uri.parse ("content:// Com.wzw.sqllitedemo.providers.personcontentprovider/person/update ");//Gets the content provider Access Object Contentresolver resolver = GetContext (). Getcontentresolver (); Contentvalues values=new contentvalues () values.put ("name", "Lisi"); int count = Resolver.update (URI, values, "_id=?", New string[]{"9"}); LOG.I (Tag, "Updated:" +count);} public void Testqueryall () {//Uriuri URI for another program = Uri.parse ("content:// Com.wzw.sqllitedemo.providers.personcontentprovider/person/queryall ");//Gets the content provider Access Object Contentresolver resolver = GetContext (). Getcontentresolver (); cursor cursor = resolver.query (URI, new string[]{"_id", "name", "age"}, NULL, NULL, "_id"); if (cursor!=null&& Cursor.getcount () >0) {int id; String name;int Age;while (Cursor.movetonext ()) {id=cursor.getint (0); name=cursor.getstring (1); Age=cursor.getint (2) ; LOG.I (tag, "ID:" +id+ "Name:" +name+ "Age:" +age);} Cursor.close ();}}}

After the first project is deployed. The content provider exists on the phone, and then tests it with a second project.


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.