外键约束在 sqlite android studio 中不起作用

Foreign key constraints not working in sqlite android studio

I have two tables. TABLE_ADD_SUBSTATION is parent table and TABLE_ADD_FEEDER is child table. I am able to add data in child table in the foreign key which does not even exist in parent table. There is no error while inserting in foreign key. What is wrong here? Am I missing something. I am new to android studio.

public class DBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "DATABASE.db";
    private static final String TABLE_ADD_SUBSTATION = "ADD_SUBSTATION";
    private static final String TABLE_ADD_FEEDER = "ADD_FEEDER";
    private static final int DATABASE_VERSION = 3;
public DBHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onConfigure(SQLiteDatabase db) {
        db.setForeignKeyConstraintsEnabled(true);
        super.onConfigure(db);
    }
 @Override
    public void onCreate(SQLiteDatabase db) {
//table for adding substation
        String createAddSubstationTable = "create table " + TABLE_ADD_SUBSTATION
                + "(substationNo INT(5) PRIMARY KEY, substationName VARCHAR(30), type VARCHAR(3), "
                + "serialNo INT(10), dateOfInstallation VARCHAR, totalCapacity INT, circle VARCHAR(10), "
                + "location VARCHAR(20), incomingSubstation int, newFeeder VARCHAR(10), newMeter VARCHAR(10))";
        db.execSQL(createAddSubstationTable);

        //table for adding feeder
        String createAddFeederTable = "create table " + TABLE_ADD_FEEDER
                +"(feederNo INT(5) PRIMARY KEY, typeOfConductor VARCHAR(30), conductorCapacity INT(10), incomingLine INT(10), "
                + "outgoingLine INT(10), totalLoad INT(10), totalNoOfConnection INT(10), status INT,"
                + "feederName VARCHAR(30), feederLength INT(10), substationNo INT(5) NOT NULL,"
                + " FOREIGN KEY(substationNo) REFERENCES TABLE_ADD_SUBSTATION(substationNo))";
        db.execSQL(createAddFeederTable);
}

 @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists " + TABLE_ADD_SUBSTATION);
        db.execSQL("drop table if exists " + TABLE_ADD_FEEDER);
       
        //create table again
        onCreate(db);
    }


 //function to add a substation into database
    public char addSubstationIntoDatabase(int substationNo, String substationName, String type, int serialNo,String dateOfInstallation, int totalCapacity, String circle, String location,
                                   int incomingSubstation){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put("substationNo", substationNo);
        contentValues.put("substationName",substationName);
        contentValues.put("type", type);
        contentValues.put("serialNo",serialNo);
        contentValues.put("dateOfInstallation", dateOfInstallation);
        contentValues.put("totalCapacity",totalCapacity);
        contentValues.put("circle", circle);
        contentValues.put("location", location);
        contentValues.put("incomingSubstation", incomingSubstation);

        long result = db.insert(TABLE_ADD_SUBSTATION, null, contentValues);
        if(result == -1){
            Cursor cursor = db.rawQuery("Select substationNo from ADD_SUBSTATION where substationNo = ?", new String[]{String.valueOf(substationNo)});
            if(cursor.getCount()>0)
                return 'B';
            else return 'C';
        }
        else
            return 'D';

    }



 //function to add feeder into database
    public boolean addFeederIntoDatabase(int feederNo,String feederName, int feederLength, String typeOfConductor,
                                         int conductorCapacity, int incomingLine, int outgoingLine, int totalLoad,
                                         int totalNoOfConnection, int status, int substationNo){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();


        contentValues.put("feederNo", feederNo);
        contentValues.put("feederName",feederName);
        contentValues.put("feederLength", feederLength);
        contentValues.put("typeOfConductor", typeOfConductor);
        contentValues.put("conductorCapacity", conductorCapacity);
        contentValues.put("incomingLine", incomingLine);
        contentValues.put("outgoingLine",outgoingLine);
        contentValues.put("totalLoad", totalLoad);
        contentValues.put("totalNoOfConnection", totalNoOfConnection);
        contentValues.put("status", status);
        contentValues.put("substationNo", substationNo);
        Log.d("contentValues", String.valueOf(contentValues));

        long result = db.insert(TABLE_ADD_FEEDER, null, contentValues);
        db.close();
        if(result == -1)
            return false;
        else
            return true;
}```


在方法 onCreate() 中,您通过连接 sql 关键字、table 名称和列名称来构造字符串 createAddFeederTable
但是您使用 TABLE_ADD_SUBSTATION 作为列 substationNo 引用的 table 的名称,这是错误的。
TABLE_ADD_SUBSTATION 是一个变量而不是 table 的名称。

将您的代码更改为:

String createAddFeederTable = "create table " + TABLE_ADD_FEEDER
        +"(feederNo INT(5) PRIMARY KEY, typeOfConductor VARCHAR(30), conductorCapacity INT(10), incomingLine INT(10), "
        + "outgoingLine INT(10), totalLoad INT(10), totalNoOfConnection INT(10), status INT,"
        + "feederName VARCHAR(30), feederLength INT(10), substationNo INT(5) NOT NULL,"
        + "FOREIGN KEY(substationNo) REFERENCES "
        + TABLE_ADD_SUBSTATION
        + "(substationNo))";
db.execSQL(createAddFeederTable);

此更改后,从设备上卸载应用程序以删除数据库并重新运行以重新创建它。

此外,请注意数据类型 INT(10)VARCHAR 实际上并不存在于 SQLite 中,尽管您可以使用它们。
相反,您应该使用 INTEGERTEXT.