[英]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 .
TABLE_ADD_SUBSTATION是父表, TABLE_ADD_FEEDER是子表。 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.
我是 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;
}```
In the method onCreate()
you construct the string createAddFeederTable
by concatenating sql keywords, table names and column names.在
onCreate()
方法中,您通过连接 sql 关键字、表名和列名来构造字符串createAddFeederTable
。
But you use TABLE_ADD_SUBSTATION
as the name of the table which is referenced by the column substationNo
and this is wrong.但是您使用
TABLE_ADD_SUBSTATION
作为列substationNo
引用的表的名称,这是错误的。
TABLE_ADD_SUBSTATION
is a variable and not the table's name. TABLE_ADD_SUBSTATION
是一个变量而不是表的名称。
Change your code to this:将您的代码更改为:
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);
After this change, uninstall the app from the device to delete the database and rerun to recreate it.在此更改后,从设备上卸载应用程序以删除数据库并重新运行以重新创建它。
Also, note that the data types INT(10)
and VARCHAR
do not actually exist in SQLite, although you can use them.另外,请注意数据类型
INT(10)
和VARCHAR
在 SQLite 中实际上并不存在,尽管您可以使用它们。
Instead you should use INTEGER
and TEXT
.相反,您应该使用
INTEGER
和TEXT
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.