Niroj thapa Niroj thapa - 3 months ago 9
Android Question

Android SQLite ForeignKey Column not found in Database

After the program runs Successfully and i extract

.db
file from Android Device monitor,I can find all my Tables and columns for each tables except the Column for
Foreign key
.Can anyone why this is happening? Is there something wrong with my
Query
? Here is my Code:

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

private static final String Table_Survey = "SURVEY";
private static final String Survey_KEY_ID = "id";
private static final String Survey_KEY_NAME = "Survey_Name";
private static final String KEY_Table_Name = "Table_Name";


private static final String Table_formDetails = "FormDetails";
private static final String formDetails_KEY_ID = "id";
private static final String formDetails_field_Type="Field_Type";
private static final String formDetails_label = "Label";
private static final String formDetails_Options = "Options";

public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

String CREATE_SURVEY_TABLE = "CREATE TABLE " + Table_Survey + "("
+ Survey_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + Survey_KEY_NAME + " VARCHAR(30),"
+ KEY_Table_Name + " VARCHAR(50)" + ")";
db.execSQL(CREATE_SURVEY_TABLE);


String CREATE_FORM_DETAILS_TABLE = "CREATE TABLE " + Table_formDetails + "("
+ formDetails_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + formDetails_field_Type + " VARCHAR(30),"
+ formDetails_Options + " VARCHAR(30),"
+ formDetails_label + " VARCHAR(30),"
+ " FOREIGN KEY ("+Survey_KEY_ID+") REFERENCES "+Table_Survey+"("+Survey_KEY_ID+"));";
db.execSQL(CREATE_FORM_DETAILS_TABLE);



}

Answer

its because you have not added survey_key_id in CREATE_FORM_DETAILS_TABLE

String CREATE_FORM_DETAILS_TABLE = "CREATE TABLE " + Table_formDetails + "("
            + formDetails_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + formDetails_field_Type + " VARCHAR(30),"
            + formDetails_Options + " VARCHAR(30),"
            + formDetails_label + " VARCHAR(30),"
            + Survey_KEY_ID + " INTEGER,"
            + " FOREIGN KEY ("+Survey_KEY_ID+") REFERENCES "+Table_Survey+"("+Survey_KEY_ID+"));";
    db.execSQL(CREATE_FORM_DETAILS_TABLE);