SQLite
 sql >> Base de données >  >> RDS >> SQLite

Android :Comment rejoindre la table enfant avec la table parent SQLite

vous pouvez utiliser le nom du collège comme clé étrangère dans la table des étudiants assurez-vous que vos deux tables de colonnes ont un nom de colonne différent assurez-vous simplement que COLUMN_ID ="id" que COLUMN_ID ="s_id"

// Nom de la base de donnéespublic static final String DATABASE_NAME ="details.db" ;

        // Table 1
        public static final String TABLE_NAME = "CollegeName";
        public static final String COLUMN_ID = "c_ID";
        public static final String COLUMN_TITLE = "college_NAME";
        private static final String COLUMN_IMAGE = "image_bitmap";
    
        // Table 2
        private static final String TABLE2_NAME = "studentsName";
        public static final String COLUMN1_ID = "s_ID";
        public static final String COLUMN2_TITLE = "students_NAME";


public void onCreate(SQLiteDatabase sqLiteDatabase) {

        String query =
                "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
                        + COLUMN_ID + " INTEGER PRIMARY KEY  ,"
                         + COLUMN_TITLE + " TEXT, "
                + COLUMN_IMAGE + " BLOB );";

        sqLiteDatabase.execSQL(query);

        String query1 =
                "CREATE TABLE IF NOT EXISTS " + TABLE2_NAME + "("
                        + COLUMN1_ID + " INTEGER PRIMARY KEY ,"
                        + COLUMN2_TITLE + "  TEXT ,"
                        + COLUMN_C_ID + " INTEGER, " + "FOREIGN KEY("+ 
                   COLUMN_C_ID +") " 
     + "REFERENCES " + TABLE_NAME +"("+COLUMN_ID +")"+ ");";


        sqLiteDatabase.execSQL(query1);

    }