android - Sqlite foreign key -
i have 4 tables namely driveover, tyres, error , vehicle in database. when insert data drive on table, drive on id referenced in table tyres drive_log. when try adding data database, drive_log remains null , number enter valid though not exist in drive_over. searched online solutions , read foreign keys have turned on off default. added onconfigure method in sqliteopenhelper class , when tried insert invalid data in drive_log foreign key constraint not fail. pragma foreign_key=0; returned when tried check if turned on via command line on sqlite guessing foreign keys constraint not being enabled. question is, how turn on foreign keys?
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); } } }
you miss space after each of drop table statements.
won't allow deleting existing tables, , tables won't recreated.
this
@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); } }
should be
@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); } }
also, in posted code, there's missing closing bracket (}), needed close containing class.
please note public static final int database_version = 1;
must increased each time make strucural changes database (i.e.: adding column table).
Comments
Post a Comment