注意 本篇实在sqlite的基础上编写的所以建议首先了解sqlite
首先建立两个模块
ContentProvider ContentResolver
ContentProvider 里面需要建立表和建立连接 所以在这里需要建立DBHelp类
DBHelp代码如下所示
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelp extends SQLiteOpenHelper {
public DBHelp(Context context) {
super(context, "godv.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//建表
sqLiteDatabase.execSQL("create table godv(_id integer primary key autoincrement,name varchar)");
//插入初始化数据
sqLiteDatabase.execSQL("insert into godv (name) values ('godv')");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
在ContentProvider下建立GodvProvider类继承ContentProvider代码:
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
/**
* 操作Godv表的类
*/
public class GodvProvider extends ContentProvider {
public GodvProvider() {
}
@Override
public boolean onCreate() {
return false;
}
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
return null;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
return null;
}
@Override
public int delete(Uri uri, String s, String[] strings) {
return 0;
}
@Override
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
return 0;
}
}
注册provider authorities全类名 小写 exported让别的应用可访问
写ContentProvider内部的实现
首先需要添加合法的URI
在GodvProvider里面定义静态变量 matcher 用来存放所有合法Uri容器
在static块中添加合法的uri
public class GodvProvider extends ContentProvider {
//code如果没有匹配 返回的值 存放Uri的容器
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
private DBHelp dbHelp;
//保存一些合法的Uri
//content://com.example.contentprovider.godvprovider(authority)/Godv(表名)
//content://com.example.contentprovider.godvprovider(authority)/Godv(表名)/3(id)
//参数一 在AndroidManifest.xml文件中定义的authority
static {
matcher.addURI("com.example.contentprovider.godvprovider", "/Godv", 1);
//#匹配任意数字
matcher.addURI("com.example.contentprovider.godvprovider", "/Godv/#", 2);
}
public GodvProvider() {
}
@Override
public boolean onCreate() {
dbHelp = new DBHelp(getContext());
return false;
}
查询
补全GodvProvider写查询的代码
//content://com.example.contentprovider.godvprovider/Godv 不根据id操作
//content://com.example.contentprovider.godvprovider/Godv/3 根据id操作
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
//根据不同uri查询 匹配Uri 返回对应code
//得到sqlLiteDataBase
SQLiteDatabase database = dbHelp.getReadableDatabase();
Cursor cursor;
int code = matcher.match(uri);
if (code == 1) { //不根据id查询
cursor = database.query("Godv", strings, s, strings1, s1, null, null, null);
} else if (code == 2) {//根据id查询
//得到id
long id = ContentUris.parseId(uri);
cursor = database.query("Godv", strings, "_id=?", new String[]{id + ""}, null, null, null);
} else {
throw new RuntimeException("查询的uri不合法");
}
return cursor;
}
回到ContentResolver中写查询
public void query(View view) {
//得到ContentResolver 对象
ContentResolver resolver = getContentResolver();
//查询 得到cursor 根据id查询
Uri uri = Uri.parse("content://com.example.contentprovider.godvprovider/Godv/1");
//第二个参数 返回字段 null为全部 因为根据id查询所以后面参数一律为null
Cursor cursor = resolver.query(uri, null, null, null, null, null);
//取出数据显示
if (cursor.moveToNext()) {
String name = cursor.getString(1);
int id = cursor.getInt(0);
Toast.makeText(this, id + ":" + name, Toast.LENGTH_LONG).show();
}
}
查询完成
插入
补全GodvProvider写插入的代码
//插入没有根据id插入的
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
//得到连接
SQLiteDatabase database = dbHelp.getReadableDatabase();
//匹配uri
int code = matcher.match(uri);
//判断合法
if (code == 1) {
long id = database.insert("Godv", null, contentValues);
//返回之前将id添加到uri当中
uri = ContentUris.withAppendedId(uri, id);
database.close();
return uri;
} else {
database.close();
throw new RuntimeException("插入的uri不合法");
}
}
回到ContentResolver中写插入
public void insert(View view) {
//得到ContentResolver 对象
ContentResolver resolver = getContentResolver();
//调用insert
Uri uri = Uri.parse("content://com.example.contentprovider.godvprovider/Godv");
ContentValues contentValues = new ContentValues();
contentValues.put("name", "jack");
Uri insert = resolver.insert(uri, contentValues);
Toast.makeText(this, insert.toString(), Toast.LENGTH_LONG).show();
}
插入完成
修改
补全GodvProvider写修改的代码
@Override
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
//得到连接
SQLiteDatabase database = dbHelp.getReadableDatabase();
//匹配uri
int code = matcher.match(uri);
int upCount = -1;
//判断合法
if (code == 1) {
//不根据id更新
upCount = database.update("Godv", contentValues, s, strings);
return upCount;
} else if (code == 2) {
long id = ContentUris.parseId(uri);
upCount = database.update("Godv", contentValues, "_id=" + id, null);
} else {
database.close();
throw new RuntimeException("更新的uri不合法");
}
database.close();
return upCount;
}
回到ContentResolver中写修改
public void update(View view) {
//得到ContentResolver 对象
ContentResolver resolver = getContentResolver();
//更新
Uri uri = Uri.parse("content://com.example.contentprovider.godvprovider/Godv/2");
ContentValues contentValues = new ContentValues();
contentValues.put("name", "jack2");
int updateCount = resolver.update(uri, contentValues, null, null);
Toast.makeText(this, "updateCount=" + updateCount, Toast.LENGTH_LONG).show();
}
修改完成
删除
补全GodvProvider写删除的代码
@Override
public int delete(Uri uri, String s, String[] strings) {
//得到连接
SQLiteDatabase database = dbHelp.getReadableDatabase();
//匹配uri
int code = matcher.match(uri);
int delCount = -1;
//判断合法
if (code == 1) {
//不根据id更新
delCount = database.delete("Godv", s, strings);
return delCount;
} else if (code == 2) {
long id = ContentUris.parseId(uri);
delCount = database.delete("Godv", "_id=" + id, null);
} else {
database.close();
throw new RuntimeException("删除的uri不合法");
}
database.close();
return delCount;
}
回到ContentResolver中写删除
public void delete(View view) {
//得到ContentResolver 对象
ContentResolver resolver = getContentResolver();
//删除
Uri uri = Uri.parse("content://com.example.contentprovider.godvprovider/Godv/2");
int deleteCount = resolver.delete(uri, null, null);
Toast.makeText(this, "deleteCount=" + deleteCount, Toast.LENGTH_LONG).show();
}
删除完成
本篇针对写代码的先后顺序进行代码展示