如何使用外键获取表数据

时间:2017-02-23 16:09:33

标签: java android sqlite android-sqlite

我在android中使用sqlite,我有4个表。 3个表使用外键指向1个表。我想使用外键获取主表数据。我使用主表中的id作为外键(不知道这是不是一个好主意,但我看到其他人做同样的事情)。

数据库助手:

/*For inserting an entry to the anime table*/
public long createAnimeEntry(String engName, String jpnName, String url, String photoUrl, String time, String description,
                             String NumberOfEpisodes){
    ContentValues initVals = new ContentValues();
    Log.v(LOG_TAG, "making a Anime entry");
    initVals.put(AnimeContract.AnimeEntry.COLUMN_ANIME_ENG_NAME, engName);
    initVals.put(AnimeContract.AnimeEntry.COLUMN_ANIME_JPN_NAME, jpnName);
    initVals.put(AnimeContract.AnimeEntry.COLUMN_ANIME_DATE, time);
    initVals.put(AnimeContract.AnimeEntry.COLUMN_ANIME_URL, url);
    initVals.put(AnimeContract.AnimeEntry.COLUMN_ANIME_IMAGE, photoUrl);
    initVals.put(AnimeContract.AnimeEntry.COLUMN_ANIME_DESCRIPTION, description);
    initVals.put(AnimeContract.AnimeEntry.COLUMN_ANIME_NUM_OF_EPISODES, NumberOfEpisodes);
    return mDb.insert(AnimeContract.AnimeEntry.TABLE_NAME_ANIME, null, initVals);
}
/*For inserting an entry to the airing table*/
public long createAiringEntry(String currentEpisode, String animeID){
    ContentValues initVals = new ContentValues();
    Log.v(LOG_TAG, "making a Airing entry");
    initVals.put(AnimeContract.AiringEntry.AIRING_ANIME_ID, animeID);
    initVals.put(AnimeContract.AiringEntry.COLUMN_AIRING_CURRENT_EPISODE, currentEpisode);
    return mDb.insert(AnimeContract.AiringEntry.TABLE_NAME_AIRING, null, initVals);
}
/*For inserting a entry to the tba table*/
public long createTBAEntry(String animeID){
    ContentValues initVals = new ContentValues();
    Log.v(LOG_TAG, "making a TBA entry");
    initVals.put(AnimeContract.TBAEntry.TBA_ANIME_ID, animeID);
    return mDb.insert(AnimeContract.TBAEntry.TABLE_NAME_TBA, null, initVals);
}

/*For inserting a entry to the fav Table*/
public long createFavoritesEntry(String animeID){
    ContentValues initVals = new ContentValues();
    Log.v(LOG_TAG, "making a FAV entry");
    initVals.put(AnimeContract.FavoritesEntry.FAV_ANIME_ID, animeID);
    return mDb.insert(AnimeContract.FavoritesEntry.TABLE_NAME_FAV, null, initVals);
}


public boolean deleteAllEntries(String tableName){
    int doneDeed = 0;
    doneDeed = mDb.delete(tableName, null, null);
    return doneDeed > 0;
}

合同:

public static final class AnimeEntry implements BaseColumns {

    public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_ANIME);

    public static final String CONTENT_LIST_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_ANIME;

    public static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_ANIME;

    /*For Anime Table*/
    public final static String TABLE_NAME_ANIME = "anime";
    public final static String ANIME_ID = BaseColumns._ID;
    public final static String COLUMN_ANIME_ENG_NAME = "engName";
    public final static String COLUMN_ANIME_JPN_NAME = "jpnName";
    public final static String COLUMN_ANIME_URL = "url";
    public final static String COLUMN_ANIME_DATE = "time";
    public final static String COLUMN_ANIME_DESCRIPTION = "description";
    public final static String COLUMN_ANIME_IMAGE = "image";
    public final static String COLUMN_ANIME_NUM_OF_EPISODES = "numOfEpisodes";

    public static boolean isValidBaseUrl(String url){
        return true;
    }

    public static Uri buildAnimeUri(long id){
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }
}
public static final class AiringEntry implements BaseColumns {

    public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_AIRING);

    public static final String CONTENT_LIST_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_AIRING;

    public static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_AIRING;

    /*For Airing Table*/
    public final static String TABLE_NAME_AIRING = "airing";
    public final static String AIRING_ID = BaseColumns._ID;
    public final static String AIRING_ANIME_ID =  "anime_id";
    public final static String COLUMN_AIRING_CURRENT_EPISODE = "engName";

    public static boolean isValidBaseUrl(String url){
        return true;
    }

    public static Uri buildAiringUri(long id){
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }
}

public static final class TBAEntry implements BaseColumns {

    public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_AIRING);

    public static final String CONTENT_LIST_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_AIRING;

    public static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_AIRING;

    /*for TBA table*/
    public final static String TABLE_NAME_TBA = "tba";
    public final static String TBA_ID = BaseColumns._ID;
    public final static String TBA_ANIME_ID = "anime_id";

    public static boolean isValidBaseUrl(String url){
        return true;
    }

    public static Uri buildTBAUri(long id){
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }
}

public static final class FavoritesEntry implements BaseColumns {

    public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_AIRING);

    public static final String CONTENT_LIST_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_AIRING;

    public static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_AIRING;

    /*for the favorites table*/
    public final static String TABLE_NAME_FAV = "fav";
    public final static String FAV_ID = BaseColumns._ID;
    public final static String FAV_ANIME_ID = "anime_id";

    public static boolean isValidBaseUrl(String url){
        return true;
    }

    public static Uri buildFavoritesUri(long id){
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }
}

在createEntry中,我不知道如何获取动漫的特定ID,我不知道如何使用外键。我有3个不同的框架来显示3个桌子播出,tba和收藏。

0 个答案:

没有答案