ContentProvider简单使用
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyOpenHelper myOpenHelper = new MyOpenHelper(this);
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
Cursor cursor = db.query("info", null, null, null, null, null, null);
if (cursor!=null && cursor.getCount()>0){
while (cursor.moveToNext()){
String name = cursor.getString(1);
int money = cursor.getInt(2);
System.out.println("----"+name+money);
}
}
}
}
public class MyDBProvider extends ContentProvider {
private MyOpenHelper myOpenHelper;
private static final int QUERY_SUCCESS=1;
private static final int INSERT_SUCCESS=2;
private static final int UPDATE_SUCCESS=3;
private static final int DELETE_SUCCESS=4;
private static final UriMatcher sUriMatcher =new UriMatcher(UriMatcher.NO_MATCH);
static {
sUriMatcher.addURI("lijinpeng","query",QUERY_SUCCESS);
sUriMatcher.addURI("lijinpeng","insert",INSERT_SUCCESS);
sUriMatcher.addURI("lijinpeng","update",UPDATE_SUCCESS);
sUriMatcher.addURI("lijinpeng","delete",DELETE_SUCCESS);
}
@Override
public boolean onCreate() {
myOpenHelper = new MyOpenHelper(getContext());
return false;
}
@Override
public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
int match = sUriMatcher.match(uri);
switch (match){
case QUERY_SUCCESS :
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
Cursor cursor = db.query("info", projection, selection, selectionArgs, null, null, sortOrder);
return cursor;
default:
return null;
}
}
@Override
public String getType( Uri uri) {
return null;
}
/*match不匹配会有异常*/
@Override
public Uri insert( Uri uri, ContentValues values) {
int match = sUriMatcher.match(uri);
switch (match){
case INSERT_SUCCESS:
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
long insert = db.insert("info",null,values);
db.close();
return Uri.parse("insert ="+insert) ;
default:
throw new IllegalArgumentException("wrong my man");
}
}
@Override
public int delete( Uri uri, String selection, String[] selectionArgs) {
int match = sUriMatcher.match(uri);
switch (match) {
case DELETE_SUCCESS:
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
int delete = db.delete("info", selection, selectionArgs);
db.close();
return delete;
default:
return -1;
}
}
@Override
public int update(Uri uri, ContentValues values,String selection, String[] selectionArgs) {
int match = sUriMatcher.match(uri);
switch (match) {
case UPDATE_SUCCESS:
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
int update = db.update("info", values, selection, selectionArgs);
db.close();
return update;
default:
return -1;
}
}
}
public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper( Context context) {
super(context, "MyDb.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table info (_id integer primary key autoincrement,name varchar(20),money integer)");
db.execSQL("insert into info(name,money) values (?,?)",new String[]{"kevin","2000"});
db.execSQL("insert into info(name,money) values (?,?)",new String[]{"wx","10000"});
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
读
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*String path= "/data/data/com.example.dbprovider_51/databases/MyDb.db";
SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
Cursor cursor = db.query("info", null, null, null, null, null, null);
if (cursor!=null && cursor.getCount()>0){
while (cursor.moveToNext()){
String name = cursor.getString(1);
int money = cursor.getInt(2);
System.out.println("--------name = "+name+"/money = "+money);
不好的方法
}
}*/
}
public void query(View view) {
Uri uri = Uri.parse("content://lijinpeng/query");
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, null);
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String name = cursor.getString(1);
int money = cursor.getInt(2);
System.out.println("--------name = " + name + "/money = " + money);
}
}
}
public void insert(View view) {
Uri uri = Uri.parse("content://lijinpeng/insert");
ContentResolver resolver = getContentResolver();
ContentValues values = new ContentValues();
values.put("name","ppp");
values.put("money","111");
Uri insert = resolver.insert(uri, values);
if (insert !=null){
Toast.makeText(this, insert.toString(), Toast.LENGTH_SHORT).show();
}
}
public void updata(View view) {
Uri uri = Uri.parse("content://lijinpeng/update");
ContentResolver resolver = getContentResolver();
ContentValues values = new ContentValues();
values.put("money","999");
int update = resolver.update(uri,values,"name =?",new String[]{"ppp"});
Toast.makeText(this,"--------update"+update, Toast.LENGTH_SHORT).show();
}
public void delete(View view) {
Uri uri = Uri.parse("content://lijinpeng/delete");
ContentResolver resolver = getContentResolver();
int delete = resolver.delete(uri, null, null);
Toast.makeText(this,"--------delete"+delete, Toast.LENGTH_SHORT).show();
}
}