Day10
public MyHelper(Context context) {
super(context, "user.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user (_id integer primary key autoincrement ,name varchar(30),age integer)");
for(int i=1;i<10;i++){
db.execSQL("insert into user values(null,?,?)",new String[]{"非常的帅"+i+"",i+""});
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package com.example.my_contentprovider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.CancellationSignal;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class MyProvider extends ContentProvider {
private MyHelper myHelper;
private SQLiteDatabase db;
//获取数据库
@Override
public boolean onCreate() {
myHelper=new MyHelper(getContext());
db=myHelper.getWritableDatabase();
if(db==null){
return false;
}else{
return true;
}
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
return db.query("user",projection,selection,selectionArgs,null,null,sortOrder);
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
db.insert("user",null,values);
return uri;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
return db.delete("user",selection,selectionArgs);
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
return db.update("user",values,selection,selectionArgs);
}
}
public class Main2Activity extends AppCompatActivity {
private ContentResolver contentResolver;
private Uri uri;
private Button buttonId;
private static final String TAG = "Main2Activity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uri=Uri.parse("content://com.bawei");
contentResolver=getContentResolver();
buttonId = findViewById(R.id.button_id);
buttonId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
query();
}
});
//
}
//user (_id integer primary key autoincrement ,name varchar(30),age integer)
public void query(){
//参数一:uri 参数二:查询的字段 参数三:条件 参数四:为?赋值 参数五:排序
Toast.makeText(this, "cha", Toast.LENGTH_SHORT).show();
Cursor cursor=contentResolver.query(uri,new String[]{"_id","name"},null,null,null);
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
Log.i(TAG, "query: "+name);
Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
}
//遍历
}
public void delete(){
//参数一:uri 参数二:条件 参数三:为?赋值
int raw=contentResolver.delete(uri,"name=?",new String[]{"非常的帅1"});
}
public void insert(){
//添加数据
ContentValues contentValues=new ContentValues();
contentValues.put("name","要甜雪");
contentValues.put("age",18);
contentResolver.insert(uri,contentValues);
//遍历
}
}