[英]Foreign key constraints not working in sqlite android studio
我有兩張桌子。 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;
}```
在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 中實際上並不存在,盡管您可以使用它們。
相反,您應該使用INTEGER
和TEXT
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.