-
ContentProvider下载
资源介绍
package com.example.tigongzhe;
import android.R.integer;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.SyncStateContract.Helpers;
import android.text.Selection;
import android.util.Log;
public class provider extends ContentProvider {
private MyOpenHelper myOpenHelper;
private SQLiteDatabase sqLiteDatabase;
private static final UriMatcher URI_MATCHER=new UriMatcher(UriMatcher.NO_MATCH);
private final String TAG="provider";
private static final String authority="com.example.tigongzhe.provider";
static {
URI_MATCHER.addURI(authority, "contacter", 1);
URI_MATCHER.addURI(authority, "contacter/#", 2);
}
private static final String _id="id";
private static final String name="name";
private static final String num="num";
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
myOpenHelper=new MyOpenHelper(getContext(), DB_Name, null, version_1);
return true;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
int flag=URI_MATCHER.match(uri);
switch (flag) {
case 2:
return "vnd.android.cursor.item/contacter";
case 1:
return "vnd.android.dir.item/contacter";
default:
throw new IllegalArgumentException("异常参数");
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
sqLiteDatabase=myOpenHelper.getWritableDatabase();
int flag=URI_MATCHER.match(uri);
switch (flag) {
case 1:
sqLiteDatabase.insert(Table_Name, name, values);
break;
case 2:
long id=sqLiteDatabase.insert(Table_Name, name, values);
ContentUris.withAppendedId(uri, id);
default:
break;
}
return uri;
}
@Override
public Cursor query(Uri uri, String[] arg1, String arg2, String[] arg3,
String arg4) {
// TODO Auto-generated method stub
Cursor cursor;
sqLiteDatabase=myOpenHelper.getReadableDatabase();
int flag=URI_MATCHER.match(uri);
switch (flag) {
case 1:
cursor=sqLiteDatabase.query(Table_Name, arg1, arg2, arg3, null, null,arg4);
break;
case 2:
long id=ContentUris.parseId(uri);
arg2=(arg2==null||"".equals(arg2.trim()))? _id+"="+id:arg2+"and"+_id+"="+id;
cursor=sqLiteDatabase.query(Table_Name, arg1, arg2, arg3, null, null,arg4);
default:
throw new IllegalArgumentException("参数错误");
}
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
int num=0;
sqLiteDatabase=myOpenHelper.getWritableDatabase();
int flag=URI_MATCHER.match(uri);
switch (flag) {
case 1:
num=sqLiteDatabase.update(Table_Name, values,selection, selectionArgs);
break;
case 2:
long id=ContentUris.parseId(uri);
selection=(selection==null||"".equals(selection.trim()))?
_id+"="+id:selection+"and"+_id+"="+id;
num=sqLiteDatabase.update(Table_Name, values,selection, selectionArgs);
default:
break;
}
return num;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
int num=0;
sqLiteDatabase=myOpenHelper.getWritableDatabase();
int flag=URI_MATCHER.match(uri);
switch (flag) {
case 1:
num=sqLiteDatabase.delete(Table_Name, selection, selectionArgs);
break;
case 2:
long id=ContentUris.parseId(uri);
selection=(selection==null||"".equals(selection.trim()))?_id+"="+id:selection+"and"+_id+"="+id;
num=sqLiteDatabase.delete(Table_Name, selection, selectionArgs);
default:
throw new IllegalArgumentException("异常参数");
}
return num;
}
private final String DB_Name = "mydb.db";
private final String Table_Name="contacter";
private final int version_1=1;
private class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
/**
* @description 当数据表无连接时创建新的表
*/
@Override
public void onCreate(SQLiteDatabase db) {
String sql = " create table if not exists " + Table_Name
+ "(id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name varchar(64),num varchar(64))";
db.execSQL(sql);
}
/**
* @description 当版本更新时触发的方法
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = " drop table if exists " + Table_Name;
db.execSQL(sql);
onCreate(db);
}
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++package com.example.tigongzhe;
import android.R.integer;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.SyncStateContract.Helpers;
import android.text.Selection;
import android.util.Log;
public class provider extends ContentProvider {
private MyOpenHelper myOpenHelper;
private SQLiteDatabase sqLiteDatabase;
private static final UriMatcher URI_MATCHER=new UriMatcher(UriMatcher.NO_MATCH);
private final String TAG="provider";
private static final String authority="com.example.tigongzhe.provider";
static {
URI_MATCHER.addURI(authority, "contacter", 1);
URI_MATCHER.addURI(authority, "contacter/#", 2);
}
private static final String _id="id";
private static final String name="name";
private static final String num="num";
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
myOpenHelper=new MyOpenHelper(getContext(), DB_Name, null, version_1);
return true;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
int flag=URI_MATCHER.match(uri);
switch (flag) {
case 2:
return "vnd.android.cursor.item/contacter";
case 1:
return "vnd.android.dir.item/contacter";
default:
throw new IllegalArgumentException("异常参数");
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
sqLiteDatabase=myOpenHelper.getWritableDatabase();
int flag=URI_MATCHER.match(uri);
switch (flag) {
case 1:
sqLiteDatabase.insert(Table_Name, name, values);
break;
case 2:
long id=sqLiteDatabase.insert(Table_Name, name, values);
ContentUris.withAppendedId(uri, id);
default:
break;
}
return uri;
}
@Override
public Cursor query(Uri uri, String[] arg1, String arg2, String[] arg3,
String arg4) {
// TODO Auto-generated method stub
Cursor cursor;
sqLiteDatabase=myOpenHelper.getReadableDatabase();
int flag=URI_MATCHER.match(uri);
switch (flag) {
case 1:
cursor=sqLiteDatabase.query(Table_Name, arg1, arg2, arg3, null, null,arg4);
break;
case 2:
long id=ContentUris.parseId(uri);
arg2=(arg2==null||"".equals(arg2.trim()))? _id+"="+id:arg2+"and"+_id+"="+id;
cursor=sqLiteDatabase.query(Table_Name, arg1, arg2, arg3, null, null,arg4);
default:
throw new IllegalArgumentException("参数错误");
}
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
int num=0;
sqLiteDatabase=myOpenHelper.getWritableDatabase();
int flag=URI_MATCHER.match(uri);
switch (flag) {
case 1:
num=sqLiteDatabase.update(Table_Name, values,selection, selectionArgs);
break;
case 2:
long id=ContentUris.parseId(uri);
selection=(selection==null||"".equals(selection.trim()))?
_id+"="+id:selection+"and"+_id+"="+id;
num=sqLiteDatabase.update(Table_Name, values,selection, selectionArgs);
default:
break;
}
return num;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
int num=0;
sqLiteDatabase=myOpenHelper.getWritableDatabase();
int flag=URI_MATCHER.match(uri);
switch (flag) {
case 1:
num=sqLiteDatabase.delete(Table_Name, selection, selectionArgs);
break;
case 2:
long id=ContentUris.parseId(uri);
selection=(selection==null||"".equals(selection.trim()))?_id+"="+id:selection+"and"+_id+"="+id;
num=sqLiteDatabase.delete(Table_Name, selection, selectionArgs);
default:
throw new IllegalArgumentException("异常参数");
}
return num;
}
private final String DB_Name = "mydb.db";
private final String Table_Name="contacter";
private final int version_1=1;
private class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
/**
* @description 当数据表无连接时创建新的表
*/
@Override
public void onCreate(SQLiteDatabase db) {
String sql = " create table if not exists " + Table_Name
+ "(id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name varchar(64),num varchar(64))";
db.execSQL(sql);
}
/**
* @description 当版本更新时触发的方法
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = " drop table if exists " + Table_Name;
db.execSQL(sql);
onCreate(db);
}
}
}