Android Content Provider,如何动态添加新数据库?

时间:2017-01-04 12:34:07

标签: android android-contentprovider

我有一个可以创建一种类型数据库的Content Provider。我的数据库包含5列和一个表,并且通过我的程序,我希望能够创建一个与我的第一个表具有相同列的新表。如何更改我的内容提供商才能执行此操作?我需要加工一些uri,但我怎么能这样做才能进入我的程序?

我的专栏是:

_id, THEME, QUESTION, REPONSE, DIFFICULTE

以下是我的内容提供商:

public class CardContentProvider extends ContentProvider {
    private StockCard stock;

    public static String authority = "com.example.jean.cartememoire.CardContentProvider";
    private static String path ="Cartes_table";

    public static final String _ID = "_id";
    public static final String THEME = "THEME";
    public static final String QUESTION = "QUESTION";
    public static final String REPONSE = "REPONSE";
    public static final String DIFFICULTE = "DIFFICULTE"; //# = un chiffre
    public static final String STOCK_TABLE = "Cartes_table";

    private static final int ID_STOCK_TABLE = 1;
    private static final int ID_THEME = 2;
    private static final int ID_QUESTION = 3;
    private static final int ID_REPONSE = 4;
    private static final int ID_DIFFICULTE = 5;
    private static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        matcher.addURI(authority, STOCK_TABLE, ID_STOCK_TABLE);
        matcher.addURI(authority, STOCK_TABLE+"/*", ID_STOCK_TABLE);

    }


    @Override
    public boolean onCreate() {
        stock = StockCard.getInstance(getContext());
        return true;
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = stock.getReadableDatabase();
        int code = matcher.match(uri);
        long id;
        Cursor cursor;

        switch (code)
        {
            case ID_STOCK_TABLE:
                cursor = db.query(STOCK_TABLE, projection, selection,
                        selectionArgs, null, null, sortOrder);
                break;
            default:
                Log.d("Uri provider =", uri.toString());
                throw new UnsupportedOperationException("Pas encore implémenté");
        }

        return cursor;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = stock.getWritableDatabase();
        int code = matcher.match(uri);
        long id;
        Uri.Builder builder = new Uri.Builder();

        switch(code)
        {
            case ID_STOCK_TABLE:
                System.out.println(values.toString());
                id = db.insert(STOCK_TABLE, null, values);
                builder.appendPath(STOCK_TABLE);
                break;
            default:
                throw new UnsupportedOperationException("Pas encore implémenté");
        }

        builder.authority(authority);
        builder = ContentUris.appendId(builder, id);

        return builder.build();
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = stock.getWritableDatabase();
        int code = matcher.match(uri);
        int i;

        switch(code)
        {
            case ID_STOCK_TABLE:
                long id = ContentUris.parseId(uri);
                i = db.delete(STOCK_TABLE, "_id=" + id, null);
                break;
            default:
                throw new UnsupportedOperationException("Pas encore implémenté");
        }

        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }
}

谢谢你们!

编辑:我说行而不是列......

0 个答案:

没有答案