I followed a tutorial online on how to create a database. I have 4 tables, drive over, tyres, error and vehicle. I also added foreign keys to it and enabled it using the onConfigure method. When I run this code, and insert some data in, the Pragma foreign key is still equal to 0. I am really confused and don't know how to proceed to.
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Database extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "allan303.db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_DRIVE_OVER = "drive_over";
public static final String DRIVE_OVER_ID = "drive_over_id";
public static final String DATE = "date";
public static final String VECHILE_LOG = "vechile_log";
public static final String ERROR_LOG = "error_log";
public static final String TABLE_VECHILES = "vechiles";
public static final String VECHILES_ID = "vechiles_id";
public static final String MODEL = "model";
public static final String YEAR = "year";
public static final String NUMBER_PLATE = "number_plate";
public static final String TABLE_TYRES = "tyres";
public static final String TYRES_ID = "tyres_id";
public static final String DRIVE = "drive_log";
public static final String AXLE = "axle";
public static final String TYRE = "tyre";
public static final String PRESSURE = "pressure";
public static final String UNITS = "units";
public static final String TABLE_ERRORS = "errors";
public static final String ERRORS_ID = "errors_id";
public static final String MESSAGE = "message";
private static final String CREATE_TABLE_VECHILES = "create table " + TABLE_VECHILES + " ("+ VECHILES_ID + " integer primary key, "
+ MODEL + " text, " + YEAR + " text, " + NUMBER_PLATE + " text " + ");";
private static final String CREATE_TABLE_ERRORS = "create table " + TABLE_ERRORS + " ("+ ERRORS_ID + " integer primary key, " +
MESSAGE + " text " + ");";
private static final String CREATE_TABLE_DRIVE_OVER = "create table " + TABLE_DRIVE_OVER+ " (" + DRIVE_OVER_ID + " integer primary key autoincrement, "
+ DATE + " text, " + VECHILE_LOG+ " integer, " + ERROR_LOG + " integer," + " FOREIGN KEY ("+VECHILE_LOG+") REFERENCES "+TABLE_VECHILES+"("+VECHILES_ID+"), "
+ " FOREIGN KEY ("+ERROR_LOG+") REFERENCES "+TABLE_ERRORS+"("+ERRORS_ID+"));";
private static final String CREATE_TABLE_TYRES = "create table "
+ TABLE_TYRES + " ("
+ TYRES_ID + " integer primary key autoincrement, "
+ DRIVE + " integer, "
+ AXLE + " integer, "
+ TYRE + " integer, "
+ PRESSURE + " integer, "
+ UNITS + " text, "
+ " FOREIGN KEY ("+DRIVE+") REFERENCES "+TABLE_DRIVE_OVER+"("+DRIVE_OVER_ID+"));";
private static Database sInstance;
public static synchronized Database getInstance(Context context){
if(sInstance == null){
sInstance = new Database(context.getApplicationContext());
}
return sInstance;
}
public Database(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onConfigure(SQLiteDatabase db){
super.onConfigure(db);
db.setForeignKeyConstraintsEnabled(true);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_TABLE_DRIVE_OVER);
db.execSQL(CREATE_TABLE_VECHILES);
db.execSQL(CREATE_TABLE_TYRES);
db.execSQL(CREATE_TABLE_ERRORS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
if(oldVersion != newVersion){
db.execSQL("DROP TABLE IF EXISTS" + TABLE_DRIVE_OVER);
db.execSQL("DROP TABLE IF EXISTS" + TABLE_VECHILES);
db.execSQL("DROP TABLE IF EXISTS" + TABLE_TYRES);
db.execSQL("DROP TABLE IF EXISTS" + TABLE_ERRORS);
onCreate(db);
}
}
Aucun commentaire:
Enregistrer un commentaire