package com.pas.sqlite.db; import com.pas.sqllitehelper.PersonSQLiteOpenHelper; import android.content.ContentProvider; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; public class PersonDBProvider extends ContentProvider { /* * uri匹配器用于匹配uri,条件不满足返回-1 */ private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); private static final int INSERT = 1; private static final int DELETE = 2; private static final int UPDATE = 3; private static final int QUERY = 4; private PersonSQLiteOpenHelper helper; static { matcher.addURI("com.pas.sqlite.personprovider", "insert", INSERT); matcher.addURI("com.pas.sqlite.personprovider", "delete", DELETE); matcher.addURI("com.pas.sqlite.personprovider", "update", UPDATE); matcher.addURI("com.pas.sqlite.personprovider", "query", QUERY); } @Override public boolean onCreate() { helper = new PersonSQLiteOpenHelper(getContext()); return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { if (matcher.match(uri) == QUERY) { // 返回查询结果集合 SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.query("persons", projection, selection, selectionArgs, null, null, sortOrder); return cursor; } else { throw new IllegalArgumentException("路径不合法,无法执行查询操作"); } } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } @Override public Uri insert(Uri uri, ContentValues values) { if (matcher.match(uri) == INSERT) { SQLiteDatabase db = helper.getReadableDatabase(); db.insert("persons", null, values); } else { throw new IllegalArgumentException("路径不合法,无法执行插入操作"); } return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { if (matcher.match(uri) == DELETE) { SQLiteDatabase db = helper.getReadableDatabase(); return db.delete("persons", selection, selectionArgs); } else { throw new IllegalArgumentException("路径不合法,无法执行删除操作"); } } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { if (matcher.match(uri) == UPDATE) { SQLiteDatabase db = helper.getReadableDatabase(); return db.update("persons",values,selection,selectionArgs); } else { throw new IllegalArgumentException("路径不合法,无法执行更新操作"); } } }
2、外部程序使用提供者代码
package com.example.providerreader; import java.sql.Array; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.view.Menu; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void del(View view) { ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.pas.sqlite.personprovider/delete"); if (1 == resolver.delete(uri, "id=?", new String[] { "487" })) { Toast.makeText(this, "删除487成功", Toast.LENGTH_SHORT).show(); } } public void update(View view) { ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.pas.sqlite.personprovider/update"); ContentValues values = new ContentValues(); values.put("name", "王刚"); if (1 == resolver.update(uri, values, "id=?", new String[] { "487" })) { Toast.makeText(this, "更新487成功", Toast.LENGTH_SHORT).show(); } } public void insert(View view) { ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.pas.sqlite.personprovider/insert"); ContentValues values = new ContentValues(); values.put("name", "策划"); values.put("number", "110"); values.put("account", "12121"); resolver.insert(uri, values); Toast.makeText(this, "增加策划成功", Toast.LENGTH_SHORT).show(); } public void click(View view) { ListView lv = (ListView) findViewById(R.id.lv); lv.setAdapter(new SimpleAdapter(this, getList(), R.layout.listitem, new String[] { "id", "name" }, new int[] { R.id.ID, R.id.Name })); } private List<Map<String, Object>> getList() { ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); // 手机的中间人 ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.pas.sqlite.personprovider/query"); Cursor cursor = resolver.query(uri, null, null, null, null); while (cursor.moveToNext()) { Map<String, Object> map = new HashMap<String, Object>(); String name = cursor.getString(cursor.getColumnIndex("name")); String id = cursor.getString(cursor.getColumnIndex("id")); map.put("id", id); map.put("name", name); data.add(map); } return data; } }