首先,第一步在AndroidManifest.xml配置
<!--内容提供者 name写的是类的名字
android:exported="true" 开启访问
-->
<provider
android:authorities="com.zking.g150831_android16_sqlite.person"
android:name="com.zking.provider.MyProvider"
android:exported="true"
></provider>
在第二步:在java代码中extends ContentProvider,有6个方法,可以进行数据的整。删,改,查,在数据库中进行查询
public class MyProvider extends ContentProvider {
private SQLiteDatabase sqLiteDatabase;
private UriMatcher uriMatcher;
private static final int PERSONS=1;
private static final int PERSON=2;
@Override
public boolean onCreate() {
DbHelper dbHelper=new DbHelper(getContext(),"G150831.db",null,2);
sqLiteDatabase = dbHelper.getReadableDatabase();
//实例化URI匹配器
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//添加匹配规则
// content://com.zking.g150831_android16_sqlite.data/datas代表查询所有
// content://com.zking.g150831_android16_sqlite.data/datas/1代表查询单个
uriMatcher.addURI("com.zking.g150831_android16_sqlite.person","persons",MyProvider.PERSONS);
uriMatcher.addURI("com.zking.g150831_android16_sqlite.person","persons/#",MyProvider.PERSON);
Log.i("text","onCreate");
return false;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
//查询所有的数据
Log.i("text","query");
int code=uriMatcher.match(uri);
switch (code) {
case MyProvider.PERSONS:
Log.i("text","query查询所有");
return sqLiteDatabase.query(true,"person",strings,s,strings1,null,null,s1,null);
case MyProvider.PERSON:
Log.i("text","query查询单个");
//获取#好的值
long id= ContentUris.parseId(uri);
return sqLiteDatabase.rawQuery("select * from person where _id=?",new String[]{id+""});
}
return null;
}
@Nullable
@Override
public String getType(Uri uri) {
Log.i("text","getType");
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
Log.i("text","insert");
return null;
}
@Override
public int delete(Uri uri, String s, String[] strings) {
Log.i("text","delete");
return 0;
}
@Override
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
Log.i("text","update");
return 0;
}
}
在第三步:在java代码中,在数据库中可以查询数据
public class MainActivity extends ListActivity {
private EditText et_main_id;
private EditText et_main_name;
private EditText et_main_age;
private SQLiteDatabase database;
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_id = (EditText) findViewById(R.id.et_main_id);
et_main_name = (EditText) findViewById(R.id.et_main_name);
et_main_age = (EditText) findViewById(R.id.et_main_age);
listview = getListView();
//GO
//Java
//php
DbHelper dbHelper=new DbHelper(this,"G150831.db",null,2);
database = dbHelper.getReadableDatabase();
//Hibernate session
//查询数据
//cursor 游标
Cursor cursor=database.query(false,"person",null,null,null,null,null,null,"2,2");
//List<Person>
//循环游标,---。List<Map<Styring,?>>
//BaseAdapter SimpleAdater
SimpleCursorAdapter simpleCursorAdapter=new SimpleCursorAdapter(this,R.layout.item_listview,cursor,new String[]{"_id","name","age"},new int[]{R.id.tv_item_list_id,R.id.tv_item_list_name,R.id.tv_item_list_age});
listview.setAdapter(simpleCursorAdapter);
// database.rawQuery()
}
public void save(View view){
String name=et_main_name.getText().toString();
String age=et_main_age.getText().toString();
//存到数据库
// ContentValues values=new ContentValues();//Map
// values.put("name",name);
// values.put("age",age);
// values.putNull("_id");
// database.insert("person","name",values);
//HQL QBC 纯(原)
for (int i = 0; i <100 ; i++) {
database.execSQL("insert into person values(null,?,?)",new String[]{name+i,age});
}
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}
}