public class youContentProviderTest {
@Test
public void delete() throws Exception {
Context context = InstrumentationRegistry.getContext();
ContentResolver contentResolver = context.getContentResolver();
Uri uri = Uri.parse("content://com.kankanla.e560.m0325a.youContentProvider/categoly/delete/");
int i = contentResolver.delete(uri, "rowid > ?", new String[]{"10"});
System.out.println("77777777777777777777777777777777");
System.out.println(i);
}
@Test
public void insert() throws Exception {
Context context = InstrumentationRegistry.getContext();
ContentResolver contentResolver = context.getContentResolver();
Uri uri = Uri.parse("content://com.kankanla.e560.m0325a.youContentProvider/category/insert/1");
System.out.println("11111111111111111111111111111111111111111");
System.out.println(uri.getAuthority());
System.out.println(uri.getPath());
System.out.println(uri.getPathSegments());
System.out.println("2222222222222222222222222222222222222222222");
ContentValues contentValues = new ContentValues();
contentValues.put("category_name", "日本語");
for (int i = 0; i < 10; i++) {
Uri uri1 = contentResolver.insert(uri, contentValues);
System.out.println(uri1.toString());
}
}
}
package com.kankanla.e560.m0325a;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by E560 on 2017/03/25.
*/
public class youContentProvider extends ContentProvider {
private static final String TAG = "youContentProvider";
private static YouDB youDB;
private static UriMatcher uriMatcher;
private static final String AUTHORITY = "com.kankanla.e560.m0325a.youContentProvider";
private static final int insert_category = 1;
private static final int select_category = 2;
private static final int insert_list = 3;
private static final int select_list = 4;
private static final int matcher_all = 5;
private static final int del_category = 6;
private static final int error = 9;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "list/insert", insert_list);
uriMatcher.addURI(AUTHORITY, "category/insert", insert_category);
uriMatcher.addURI(AUTHORITY, "category/insert/#", insert_category);
uriMatcher.addURI(AUTHORITY, "category/delete/", del_category);
uriMatcher.addURI(AUTHORITY, "*/*/*", error);
}
@Override
public boolean onCreate() {
youDB = new YouDB(getContext());
SQLiteDatabase sqLiteDatabase = youDB.getReadableDatabase();
sqLiteDatabase.isReadOnly();
System.out.println("888888888888888888888888888888888888888888");
Log.i(TAG, String.valueOf(sqLiteDatabase.isOpen()));
return sqLiteDatabase.isOpen();
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
return null;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
Uri uri1 = Uri.parse(uri.getAuthority() + uri.getPath());
int i = uriMatcher.match(uri);
System.out.println(i);
long cont = 0;
System.out.println("000000000000000000000000000000000000000000000000");
SQLiteDatabase sqLiteDatabase = youDB.getWritableDatabase();
switch (i) {
case insert_category:
cont = sqLiteDatabase.insert("category", null, values);
uri1 = ContentUris.withAppendedId(Uri.parse(uri.getAuthority() + uri.getPath()), cont);
break;
case insert_list:
cont = sqLiteDatabase.insert("list", null, values);
uri1 = ContentUris.withAppendedId(Uri.parse(uri.getAuthority() + uri.getPath()), cont);
break;
}
System.out.println(uri1.toString());
sqLiteDatabase.close();
return uri1;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
int flag = uriMatcher.match(uri);
int con = 0;
SQLiteDatabase sqLiteDatabase = youDB.getWritableDatabase();
switch (flag) {
case del_category:
con = sqLiteDatabase.delete("category", selection, selectionArgs);
break;
}
sqLiteDatabase.close();
return con;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
return 0;
}
private class YouDB extends SQLiteOpenHelper {
private static final String name = "m0325.db";
private static final int version = 1;
public YouDB(Context context) {
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql_category = "create table category(rowid INTEGER PRIMARY KEY AUTOINCREMENT, category_name TEXT NOT NULL)";
String sql_index = "create table list (rowid INTEGER PRIMARY KEY AUTOINCREMENT, category_id INTEGER NOT NULL,comment TEXT)";
db.execSQL(sql_category);
db.execSQL(sql_index);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}