目录
Monday
1.内容提供者
Tuesday
1.数据库
创建方式: (URL Authorities和文件夹路径名保持一致)
自动生成
1.新建SQL的java代码
DBaseHelper:
package com.example.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBaseHelper extends SQLiteOpenHelper {
public DBaseHelper(Context context){
super(context, "provider.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db){
//整数、主键、自动增加、类型
db.execSQL("create table info(_id integer primary key autoincrement, name varchar(20))");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){}
}
2.创建ContentProvider
MyContentProvider:
package com.example.myapplication;
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 MyContentProvider extends ContentProvider {
private static UriMatcher uriMatcher = new UriMatcher(-1);//匹配失败的返回值-1
private DBaseHelper helper;
static {
uriMatcher.addURI("com.example.mycontentprovider", "info", 1);
}
public MyContentProvider() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
if(uriMatcher.match(uri) == 1){
SQLiteDatabase database = helper.getWritableDatabase();//打开数据库
int cont = database.delete("info", selection, selectionArgs);//删除的数量
database.close();
return cont;
}
else
throw new IllegalArgumentException("Uri is error.");
}
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
// at the given URI.
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO: Implement this to handle requests to insert a new row.
if(uriMatcher.match(uri) == 1){
SQLiteDatabase database = helper.getWritableDatabase();
//空为null 否则values
database.insert("info", null, values);
database.close();
return uri;
}
else
throw new IllegalArgumentException("Uri is error.");
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
helper = new DBaseHelper(getContext());//实例化
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO: Implement this to handle query requests from clients.
if(uriMatcher.match(uri) == 1){ //匹配
SQLiteDatabase database = helper.getReadableDatabase();
//执行对数据库的查询
Cursor cursor = database.query("info", projection, selection, selectionArgs, null, null, sortOrder);
database.close();
return cursor;
}
else
throw new UnsupportedOperationException("Uri is error.");
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO: Implement this to handle requests to update one or more rows.
if(uriMatcher.match(uri) == 1){
SQLiteDatabase database = helper.getWritableDatabase();
int cont = database.update("info", values, selection, selectionArgs);
database.close();
return cont;
}
else
throw new IllegalArgumentException("Uri is error.");
}
}
activity_main:
MainActivity:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private ContentResolver resolver;
private Uri uri;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DBaseHelper helper = new DBaseHelper(this);
}
public void click(View view){
uri = Uri.parse("content://com.example.myapplication/info");
resolver = getContentResolver();
switch (view.getId()){
case R.id.add:
ContentValues values = new ContentValues();
values.put("name", "software"+count ++);
resolver.insert(uri, values);
Log.d("***", "insert");
break;
case R.id.delete:
resolver.delete(uri,"name=?", new String[]{"software0"});
Log.d("***", "delete");
break;
case R.id.query:
break;
case R.id.update:
break;
}
}
}