日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

09_Android中ContentProvider和Sqllite混合操作,一个项目调用另外一个项目的ContentProvider

發布時間:2024/9/27 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 09_Android中ContentProvider和Sqllite混合操作,一个项目调用另外一个项目的ContentProvider 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、? 編寫ContentPrivider提供者的Android應用

清單文件

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

??? package="com.itheima28.sqlitedemo"

??? android:versionCode="1"

??? android:versionName="1.0" >

?

??? <instrumentation

??????? android:name="android.test.InstrumentationTestRunner"

??????? android:targetPackage="com.itheima28.sqlitedemo" >

??? </instrumentation>

?

??? <permission android:name="aa.bb.cc.read" ></permission>

??? <permission android:name="aa.bb.cc.write" ></permission>

?

??? <uses-sdk

??????? android:minSdkVersion="8"

??????? android:targetSdkVersion="19" />

?

??? <application

??????? android:allowBackup="true"

??????? android:icon="@drawable/ic_launcher"

??????? android:label="@string/app_name"

??????? android:theme="@style/AppTheme" >

??????? <uses-library android:name="android.test.runner" />

?

??????? <activity

??????????? android:name="com.itheima28.sqlitedemo.MainActivity"

??????????? android:label="@string/app_name" >

??????????? <intent-filter>

??????????????? <action android:name="android.intent.action.MAIN" />

?

??????????????? <category android:name="android.intent.category.LAUNCHER" />

??????????? </intent-filter>

??????? </activity>

???????

??????? <!—注意加上:android:exported="true",通過它來避免出錯-à

??????? <provider

??????????? android:name=".providers.PersonContentProvider"

??????????? android:authorities="com.itheima28.sqlitedemo.providers.PersonContentProvider"

??????????? android:readPermission="aa.bb.cc.read"

??????????? android:writePermission="aa.bb.cc.write"

??????????? android:exported="true">

??????? </provider>

??? </application>

?

</manifest>

2 編寫實體Person

package com.itheima28.sqlitedemo.entities;

?

publicclass Person {

?

??? privateintid;

??? private String name;

??? privateintage;

??? publicint getId() {

?????? returnid;

??? }

??? publicvoid setId(int id) {

?????? this.id = id;

??? }

??? public String getName() {

?????? returnname;

??? }

??? publicvoid setName(String name) {

?????? this.name = name;

??? }

??? publicint getAge() {

?????? returnage;

??? }

??? publicvoid setAge(int age) {

?????? this.age = age;

??? }

??? public Person() {

?????? super();

?????? // TODO Auto-generated constructor stub

??? }

??? public Person(int id, String name, int age) {

?????? super();

?????? this.id = id;

?????? this.name = name;

?????? this.age = age;

??? }

??? @Override

??? public String toString() {

?????? return"Person [id=" + id + ", name=" + name + ", age=" + age + "]";

??? }

}

3 編寫dbHelper

package com.itheima28.sqlitedemo.db;

?

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.util.Log;

?

/**

?* @author toto

?* 數據庫幫助類,用于創建和管理數據庫的

?*/

public class PersonSQLiteOpenHelper extends SQLiteOpenHelper{

???????? private static final String TAG = "PersonSQLiteOpenHelper";

????????

???????? public PersonSQLiteOpenHelper(Context context){

?????????????????? super(context, "itheima28.db", null, 2);

???????? }

????????

???????? /**

???????? ?* 數據庫第一次創建時回調此方法

???????? ?* 初始化表

???????? ?*/

???????? @Override

???????? public void onCreate(SQLiteDatabase db) {

?????????????????? //操作數據

?????????????????? String sql = "create table person(_id integer primary key, name varchar(20), age integer);";

?????????????????? db.execSQL(sql);

???????? }

?

???????? /**

???????? ?* 數據庫的版本號更新時回調此方法

???????? ?* 更新數據庫的內容(刪除表,添加表,修改表)

???????? ?*/

???????? @Override

???????? public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

?????????????????? if (oldVersion == 1 && newVersion == 2) {

??????????????????????????? Log.i(TAG, "數據庫更新啦");

??????????????????????????? db.execSQL("alter table person add balance integer;");

?????????????????? }

???????? }

?

}

4 編寫通過sql的Dao操作類

package com.itheima28.sqlitedemo.dao;

?

import java.util.ArrayList;

import java.util.List;

?

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

?

import com.itheima28.sqlitedemo.db.PersonSQLiteOpenHelper;

import com.itheima28.sqlitedemo.entities.Person;

?

public class PersonDao {

?

???????? private PersonSQLiteOpenHelper mOpenHelper;?? // 數據庫的幫助類對象

?

???????? public PersonDao(Context context) {

?????????????????? mOpenHelper = new PersonSQLiteOpenHelper(context);

???????? }

????????

???????? /**

???????? ?* 添加到person表一條數據

???????? ?* @param person

???????? ?*/

???????? public void insert(Person person) {

?????????????????? SQLiteDatabase db = mOpenHelper.getWritableDatabase();

?????????????????? if(db.isOpen()) {??????? // 如果數據庫打開, 執行添加的操作

???????????????????????????

??????????????????????????? // 執行添加到數據庫的操作

??????????????????????????? db.execSQL("insert into person(name, age) values(?, ?);", new Object[]{person.getName(), person.getAge()});

???????????????????????????

??????????????????????????? db.close();??????? // 數據庫關閉

?????????????????? }

???????? }

????????

???????? /**

???????? ?* 更據id刪除記錄

???????? ?* @param id

???????? ?*/

???????? public void delete(int id) {

?????????????????? SQLiteDatabase db = mOpenHelper.getWritableDatabase();???????? // 獲得可寫的數據庫對象

?????????????????? if(db.isOpen()) {??????? // 如果數據庫打開, 執行添加的操作

???????????????????????????

??????????????????????????? db.execSQL("delete from person where _id = ?;", new Integer[]{id});

???????????????????????????

??????????????????????????? db.close();??????? // 數據庫關閉

?????????????????? }

???????? }

????????

?

???????? /**

???????? ?* 根據id找到記錄, 并且修改姓名

???????? ?* @param id

???????? ?* @param name

???????? ?*/

???????? public void update(int id, String name) {

?????????????????? SQLiteDatabase db = mOpenHelper.getWritableDatabase();

?????????????????? if(db.isOpen()) {??????? // 如果數據庫打開, 執行添加的操作

???????????????????????????

??????????????????????????? db.execSQL("update person set name = ? where _id = ?;", new Object[]{name, id});

???????????????????????????

??????????????????????????? db.close();??????? // 數據庫關閉

?????????????????? }

???????? }

????????

???????? public List<Person> queryAll() {

?????????????????? SQLiteDatabase db = mOpenHelper.getReadableDatabase();?????? // 獲得一個只讀的數據庫對象

?????????????????? if(db.isOpen()) {

???????????????????????????

??????????????????????????? Cursor cursor = db.rawQuery("select _id, name, age from person;", null);

???????????????????????????

??????????????????????????? if(cursor != null && cursor.getCount() > 0) {

???????????????????????????????????? List<Person> personList = new ArrayList<Person>();

???????????????????????????????????? int id;

???????????????????????????????????? String name;

???????????????????????????????????? int age;

???????????????????????????????????? while(cursor.moveToNext()) {

?????????????????????????????????????????????? id = cursor.getInt(0);??????? // 取第0列的數據 id

?????????????????????????????????????????????? name = cursor.getString(1);??? // 取姓名

?????????????????????????????????????????????? age = cursor.getInt(2);????????????? // 取年齡

?????????????????????????????????????????????? personList.add(new Person(id, name, age));

???????????????????????????????????? }

?

???????????????????????????????????? db.close();

???????????????????????????????????? return personList;

??????????????????????????? }

??????????????????????????? db.close();

?????????????????? }

?????????????????? return null;

???????? }

????????

???????? /**

???????? ?* 根據id查詢人

???????? ?* @param id

???????? ?* @return

???????? ?*/

???????? public Person queryItem(int id) {

?????????????????? SQLiteDatabase db = mOpenHelper.getReadableDatabase();?????? // 獲得一個只讀的數據庫對象

?????????????????? if(db.isOpen()) {

??????????????????????????? Cursor cursor = db.rawQuery("select _id, name, age from person where _id = ?;", new String[]{id + ""});

??????????????????????????? if(cursor != null && cursor.moveToFirst()) {

???????????????????????????????????? int _id = cursor.getInt(0);

???????????????????????????????????? String name = cursor.getString(1);

???????????????????????????????????? int age = cursor.getInt(2);

???????????????????????????????????? db.close();

???????????????????????????????????? return new Person(_id, name, age);

??????????????????????????? }

??????????????????????????? db.close();

?????????????????? }

?????????????????? return null;

???????? }

}

5 編寫不是通過sql的操作類

package com.itheima28.sqlitedemo.dao;

?

import java.util.ArrayList;

import java.util.List;

?

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.util.Log;

?

import com.itheima28.sqlitedemo.db.PersonSQLiteOpenHelper;

import com.itheima28.sqlitedemo.entities.Person;

?

public class PersonDao2 {

?

???????? private static final String TAG = "PersonDao2";

???????? private PersonSQLiteOpenHelper mOpenHelper;?? // 數據庫的幫助類對象

?

???????? public PersonDao2(Context context) {

?????????????????? mOpenHelper = new PersonSQLiteOpenHelper(context);

???????? }

????????

???????? /**

???????? ?* 添加到person表一條數據

???????? ?* @param person

???????? ?*/

???????? public void insert(Person person) {

?????????????????? SQLiteDatabase db = mOpenHelper.getWritableDatabase();

?????????????????? if(db.isOpen()) {??????? // 如果數據庫打開, 執行添加的操作

???????????????????????????

??????????????????????????? ContentValues values = new ContentValues();

??????????????????????????? values.put("name", person.getName());????????????????? // key作為要存儲的列名, value對象列的值

??????????????????????????? values.put("age", person.getAge());

??????????????????????????? long id = db.insert("person", "name", values);

??????????????????????????? Log.i(TAG, "id: " + id);

???????????????????????????

??????????????????????????? db.close();??????? // 數據庫關閉

?????????????????? }

???????? }

????????

???????? /**

???????? ?* 更據id刪除記錄

???????? ?* @param id

???????? ?*/

???????? public void delete(int id) {

?????????????????? SQLiteDatabase db = mOpenHelper.getWritableDatabase();???????? // 獲得可寫的數據庫對象

?????????????????? if(db.isOpen()) {??????? // 如果數據庫打開, 執行添加的操作

???????????????????????????

??????????????????????????? String whereClause = "_id = ?";

??????????????????????????? String[] whereArgs = {id + ""};

??????????????????????????? int count = db.delete("person", whereClause, whereArgs);

??????????????????????????? Log.i(TAG, "刪除了: " + count + "行");

??????????????????????????? db.close();??????? // 數據庫關閉

?????????????????? }

???????? }

????????

???????? /**

???????? ?* 根據id找到記錄, 并且修改姓名

???????? ?* @param id

???????? ?* @param name

???????? ?*/

???????? public void update(int id, String name) {

?????????????????? SQLiteDatabase db = mOpenHelper.getWritableDatabase();

?????????????????? if(db.isOpen()) {??????? // 如果數據庫打開, 執行添加的操作

??????????????????????????? ContentValues values = new ContentValues();

??????????????????????????? values.put("name", name);

???????????????????????????

??????????????????????????? int count? = db.update("person", values, "_id = ?", new String[]{id + ""});

?

??????????????????????????? Log.i(TAG, "修改了: " + count + "行");

???????????????????????????

??????????????????????????? db.close();??????? // 數據庫關閉

?????????????????? }

???????? }

????????

???????? public List<Person> queryAll() {

?????????????????? SQLiteDatabase db = mOpenHelper.getReadableDatabase();?????? // 獲得一個只讀的數據庫對象

?????????????????? if(db.isOpen()) {

??????????????????????????? String[] columns = {"_id", "name", "age"};???? // 需要的列

??????????????????????????? String selection = null;???? // 選擇條件, 給null查詢所有

??????????????????????????? String[] selectionArgs = null;?? // 選擇條件的參數, 會把選擇條件中的? 替換成數據中的值

??????????????????????????? String groupBy = null;?????? // 分組語句? group by name

??????????????????????????? String having = null; // 過濾語句

??????????????????????????? String orderBy = null;?????? // 排序

???????????????????????????

??????????????????????????? Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy);

???????????????????????????

??????????????????????????? int id;

??????????????????????????? String name;

??????????????????????????? int age;

??????????????????????????? if(cursor != null && cursor.getCount() > 0) {

???????????????????????????????????? List<Person> personList = new ArrayList<Person>();

????????????????????????????????????

???????????????????????????????????? while(cursor.moveToNext()) {? // 向下移一位, 知道最后一位, 不可以往下移動了, 停止.

?????????????????????????????????????????????? id = cursor.getInt(0);

?????????????????????????????????????????????? name = cursor.getString(1);

?????????????????????????????????????????????? age = cursor.getInt(2);

??????????????????????????????????????????????

?????????????????????????????????????????????? personList.add(new Person(id, name, age));

???????????????????????????????????? }

????????????????????????????????????

???????????????????????????????????? db.close();

???????????????????????????????????? return personList;

??????????????????????????? }

??????????????????????????? db.close();

?????????????????? }

?????????????????? return null;

???????? }

????????

???????? /**

???????? ?* 根據id查詢人

???????? ?* @param id

???????? ?* @return

???????? ?*/

???????? public Person queryItem(int id) {

?????????????????? SQLiteDatabase db = mOpenHelper.getReadableDatabase();?????? // 獲得一個只讀的數據庫對象

?????????????????? if(db.isOpen()) {

??????????????????????????? String[] columns = {"_id", "name", "age"};???? // 需要的列

??????????????????????????? String selection = "_id = ?";???? // 選擇條件, 給null查詢所有

??????????????????????????? String[] selectionArgs = {id + ""};???? // 選擇條件的參數, 會把選擇條件中的? 替換成數據中的值

??????????????????????????? String groupBy = null;?????? // 分組語句? group by name

??????????????????????????? String having = null; // 過濾語句

??????????????????????????? String orderBy = null;?????? // 排序

???????????????????????????

??????????????????????????? Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy);

???????????????????????????

??????????????????????????? if(cursor != null && cursor.moveToFirst()) {????????????? // cursor對象不為null, 并且可以移動到第一行

???????????????????????????????????? int _id = cursor.getInt(0);

???????????????????????????????????? String name = cursor.getString(1);

???????????????????????????????????? int age = cursor.getInt(2);

????????????????????????????????????

???????????????????????????????????? db.close();

???????????????????????????????????? return new Person(_id, name, age);

??????????????????????????? }

??????????????????????????? db.close();

?????????????????? }

?????????????????? return null;

???????? }

}

6 編寫通過sql操作數據庫的單元測試

package com.itheima28.sqlitedemo.test;

?

import java.util.List;

?

import android.database.sqlite.SQLiteDatabase;

import android.test.AndroidTestCase;

import android.util.Log;

?

import com.itheima28.sqlitedemo.dao.PersonDao;

import com.itheima28.sqlitedemo.db.PersonSQLiteOpenHelper;

import com.itheima28.sqlitedemo.entities.Person;

?

public class TestCase extends AndroidTestCase {

?

???????? private static final String TAG = "TestCase";

?

???????? public void test() {

?????????????????? // 數據庫什么時候創建

?????????????????? PersonSQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext());

??????????????????

?????????????????? // 第一次連接數據庫時創建數據庫文件. onCreate會被調用

?????????????????? openHelper.getReadableDatabase();

???????? }

????????

???????? public void testInsert() {

?????????????????? PersonDao dao = new PersonDao(getContext());

??????????????????

?????????????????? for (int i = 0; i < 20; i++) {

??????????????????????????? dao.insert(new Person(0, "冠希" + i, 10 + i));

?????????????????? }

???????? }

????????

???????? public void testDelete() {

?????????????????? PersonDao dao = new PersonDao(getContext());

?????????????????? dao.delete(1);

???????? }

?

???????? public void testUpdate() {

?????????????????? PersonDao dao = new PersonDao(getContext());

?????????????????? dao.update(3, "鳳姐");

???????? }

?

???????? public void testQueryAll() {

?????????????????? PersonDao dao = new PersonDao(getContext());

?????????????????? List<Person> personList = dao.queryAll();

??????????????????

?????????????????? for (Person person : personList) {

??????????????????????????? Log.i(TAG, person.toString());

?????????????????? }

???????? }

?

???????? public void testQueryItem() {

?????????????????? PersonDao dao = new PersonDao(getContext());

?????????????????? Person person = dao.queryItem(4);

?????????????????? Log.i(TAG, person.toString());

???????? }

????????

???????? public void testTransaction() {

?????????????????? PersonSQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext());

?????????????????? SQLiteDatabase db = openHelper.getWritableDatabase();

??????????????????

?????????????????? if(db.isOpen()) {

??????????????????????????? try {

???????????????????????????????????? // 開啟事務

???????????????????????????????????? db.beginTransaction();

????????????????????????????????????

???????????????????????????????????? // 1. 從張三賬戶中扣1000塊錢

???????????????????????????????????? db.execSQL("update person set balance = balance - 1000 where name = 'zhangsan';");

????????????????????????????????????

???????????????????????????????????? // ATM機, 掛掉了.

???????????????????????????????????? // int result = 10 / 0;

????????????????????????????????????

???????????????????????????????????? // 2. 向李四賬戶中加1000塊錢

???????????????????????????????????? db.execSQL("update person set balance = balance + 1000 where name = 'lisi';");

????????????????????????????????????

???????????????????????????????????? // 標記事務成功

???????????????????????????????????? db.setTransactionSuccessful();

??????????????????????????? } finally {

???????????????????????????????????? // 停止事務

???????????????????????????????????? db.endTransaction();

??????????????????????????? }

??????????????????????????? db.close();

?????????????????? }

???????? }

????????

???????? public void testTransactionInsert() {

?????????????????? PersonSQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext());

?????????????????? SQLiteDatabase db = openHelper.getWritableDatabase();

??????????????????

?????????????????? if(db.isOpen()) {

???????????????????????????

??????????????????????????? // 1. 記住當前的時間

??????????????????????????? long start = System.currentTimeMillis();

???????????????????????????

??????????????????????????? // 2. 開始添加數據

??????????????????????????? try {

???????????????????????????????????? db.beginTransaction();

???????????????????????????????????? for (int i = 0; i < 10000; i++) {

?????????????????????????????????????????????? db.execSQL("insert into person(name, age, balance) values('wang" + i + "', " + (10 + i) + ", " + (10000 + i) + ")");

???????????????????????????????????? }

???????????????????????????????????? db.setTransactionSuccessful();

??????????????????????????? } finally {

???????????????????????????????????? db.endTransaction();

??????????????????????????? }

???????????????????????????

??????????????????????????? // 3. 記住結束時間, 計算耗時時間

??????????????????????????? long end = System.currentTimeMillis();

???????????????????????????

??????????????????????????? long diff = end - start;

??????????????????????????? Log.i(TAG, "耗時: " + diff + "毫秒");

???????????????????????????

??????????????????????????? db.close();

?????????????????? }

???????? }

}

7 編寫不是通過sql操作的數據庫的單元測試

package com.itheima28.sqlitedemo.test;

?

import java.util.List;

?

import android.test.AndroidTestCase;

import android.util.Log;

?

import com.itheima28.sqlitedemo.dao.PersonDao2;

import com.itheima28.sqlitedemo.db.PersonSQLiteOpenHelper;

import com.itheima28.sqlitedemo.entities.Person;

?

public class TestCase2 extends AndroidTestCase {

?

???????? private static final String TAG = "TestCase";

?

???????? public void test() {

?????????????????? // 數據庫什么時候創建

?????????????????? PersonSQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext());

??????????????????

?????????????????? // 第一次連接數據庫時創建數據庫文件. onCreate會被調用

?????????????????? openHelper.getReadableDatabase();

???????? }

????????

???????? public void testInsert() {

?????????????????? PersonDao2 dao = new PersonDao2(getContext());

?????????????????? dao.insert(new Person(0, "zhouqi", 88));

???????? }

????????

???????? public void testDelete() {

?????????????????? PersonDao2 dao = new PersonDao2(getContext());

?????????????????? dao.delete(8);

???????? }

?

???????? public void testUpdate() {

?????????????????? PersonDao2 dao = new PersonDao2(getContext());

?????????????????? dao.update(3, "fengjie");

???????? }

?

???????? public void testQueryAll() {

?????????????????? PersonDao2 dao = new PersonDao2(getContext());

?????????????????? List<Person> personList = dao.queryAll();

??????????????????

?????????????????? for (Person person : personList) {

??????????????????????????? Log.i(TAG, person.toString());

?????????????????? }

???????? }

?

???????? public void testQueryItem() {

?????????????????? PersonDao2 dao = new PersonDao2(getContext());

?????????????????? Person person = dao.queryItem(4);

?????????????????? Log.i(TAG, person.toString());

???????? }

}

8 Activity不用寫東西

?

二、第二個測試項目調用第一個項目的ContentProvider

清單文件如下:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

??? package="com.itheima28.othercontentprovider"

??? android:versionCode="1"

??? android:versionName="1.0" >

???

??? <instrumentation

??????? android:name="android.test.InstrumentationTestRunner"

??????? android:targetPackage="com.itheima28.othercontentprovider">

??? </instrumentation>

??? <uses-permission android:name="aa.bb.cc.read"/>

??? <uses-permission android:name="aa.bb.cc.write"/>

???

??? <uses-sdk

??????? android:minSdkVersion="8"

??????? android:targetSdkVersion="19" />

?

??? <application

??????? android:allowBackup="true"

??????? android:icon="@drawable/ic_launcher"

??????? android:label="@string/app_name"

??????? android:theme="@style/AppTheme" >

??????? <uses-library android:name="android.test.runner"/>

???????

??????? <activity

??????????? android:name="com.itheima28.othercontentprovider.MainActivity"

??????????? android:label="@string/app_name" >

??????????? <intent-filter>

??????????????? <action android:name="android.intent.action.MAIN" />

?

??????????????? <category android:name="android.intent.category.LAUNCHER" />

??????????? </intent-filter>

??????? </activity>

??? </application>

?

</manifest>

2 Activity不用寫

3 單元測試類如下:

package com.itheima28.othercontentprovider;

?

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;

?

publicclass TextCase extends AndroidTestCase {

?

??? privatestaticfinal String TAG = "TextCase";

???

??? publicvoid testInsert() {

?????? Uri uri = Uri.parse("content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/insert");

??????

?????? //內容提供者訪問對象

?????? ContentResolver resolver = getContext().getContentResolver();

??????

?????? ContentValues values = new ContentValues();

?????? values.put("name", "fengjie");

?????? values.put("age", 90);

??????

?????? uri = resolver.insert(uri, values);

?????? Log.i(TAG, "uri:" + uri);

?????? long id = ContentUris.parseId(uri);

?????? Log.i(TAG, "添加到:" + id);

??? }

???

??? publicvoid testDelete() {

?????? Uri uri = Uri.parse("content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/delete");

??????

?????? //內容提供者訪問對象

?????? ContentResolver resolver = getContext().getContentResolver();

??????

?????? String where = "_id = ?";

?????? String[] selectionArgs = {"21"};

?????? int count = resolver.delete(uri, where, selectionArgs);

?????? Log.i(TAG, "刪除行:" + count);

??? }

???

??? publicvoid testUpdate() {

?????? Uri uri = Uri.parse("content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/update");

???

?????? //內容提供者訪問對象

?????? ContentResolver resolver = getContext().getContentResolver();

??????

?????? ContentValues values = new ContentValues();

?????? values.put("name", "lisi");

??????

?????? int count = resolver.update(uri, values,"_id = ?", new String[]{"20"});

?????? Log.i(TAG, "更新行:" + count);

??? }

???

??? publicvoid testQueryAll() {

?????? Uri uri = Uri.parse("content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/queryAll");

??????

?????? //內容提供者訪問對象

?????? ContentResolver resolver = getContext().getContentResolver();

?????? Cursor cursor = resolver.query(uri, new String[]{"_id", "name", "age"}, null, null, "_id desc");

??????

?????? 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();

?????? }

??? }

???

??? publicvoid testQuerySingleItem() {

?????? Uri uri = Uri.parse("content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/query/#");

??????

?????? // uri的末尾添加一個id content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/query/20

?????? uri = ContentUris.withAppendedId(uri, 20);

??????

?????? //內容提供者訪問對象

?????? ContentResolver resolver = getContext().getContentResolver();

?????? Cursor cursor = resolver.query(uri, new String[]{"_id", "name", "age"}, null, null, null);

?????? if (cursor != null && cursor.moveToFirst()) {

?????????? int id = cursor.getInt(0);

?????????? String name = cursor.getString(1);

?????????? int age = cursor.getInt(2); ?????

?????????? cursor.close();

?????????? Log.i(TAG, "id: " + id + ", name: " + name + ", age: " + age);

?????? }

??? }

}

?

總結

以上是生活随笔為你收集整理的09_Android中ContentProvider和Sqllite混合操作,一个项目调用另外一个项目的ContentProvider的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。