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

时间:2021-07-01 08:00:27

标签: java android sqlite android-sqlite

<块引用>

我有两张桌子。 TABLE_ADD_SUBSTATION父表TABLE_ADD_FEEDER子表。我可以在外键的子表中添加数据,而外键甚至不存在于父表中。插入外键时没有错误。这里有什么问题?我是不是错过了什么。我是 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;
}```


1 个答案:

答案 0 :(得分:0)

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

将您的代码更改为:

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

相关问题