将列从一个表复制到另一个表,忽略重复项

时间:2016-10-30 22:11:56

标签: android sqlite android-sqlite

表1:单词

__________________________________________________________________________
| word_id | word | category_title |             definition               |
|---------|------|----------------|--------------------------------------|
|    1    | cat  |    animals     |    a four-legged feline creature     |   
|    2    | bob  | famous people  |    a very famous reggae artist       |
|    3    | dog  |    animals     | a four_legged hound, enemy of feline |
--------------------------------------------------------------------------

表2:类别

________________________________________________
| category_id | category_title |     imguri     | 
|-------------|----------------|----------------|
|      1      |     animals    | animals_banner |
|      2      | famous people  | fms_ppl_banner |
-------------------------------------------------

我的代码:

public class MyDBHandler extends SQLiteOpenHelper{

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "Memoplay.db";

    public static final String TABLE_NAME_WORDS = "words";
    public static final String COLUMN_WORD_ID = "word_id";
    public static final String COLUMN_WORD = "word";
    public static final String COLUMN_CATEGORY = "categorytitle";
    public static final String COLUMN_DEFINITION = "definiton";


    public static final String TABLE_NAME_CATEGORIES = "categories";
    public static final String COLUMN_CATEGORY_ID = "category_id";
    //public static final String COLUMN_CATEGORY = "categorytitle";
    public static final String COLUMN_IMGURI = "imguri";

    //constuctor method
    public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String createWordsTableQuery = "CREATE TABLE" +
                TABLE_NAME_WORDS + "(" +
                COLUMN_WORD_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," +
                COLUMN_WORD + "TEXT," +
                COLUMN_DEFINITION + "TEXT," +
                COLUMN_CATEGORY + "TEXT" + ");";
        db.execSQL(createWordsTableQuery);

        String createCategoriesTable = "CREATE TABLE" +
                TABLE_NAME_CATEGORIES + "(" +
                COLUMN_CATEGORY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT" +
                COLUMN_CATEGORY + "TEXT" +
                COLUMN_IMGURI + "TEXT" + ");";
        db.execSQL(createCategoriesTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

我的目标:

我想为单词表中找到的唯一类别生成一个表,我知道stackOverflow上有类似的答案可以将列复制到新表中,但是没有一个(我能找到)解决了如何关联新表像“img uri”这样的数据。以及每个复制值的唯一主键。

我对sqlite和android很新,任何帮助都会非常感激 感谢。

0 个答案:

没有答案