android - why I can not use tables with 'foreign key'? -
when create 2 tables, without foreign key works fine, when add foreign key, app doesn't create tables.
please me.
//table1 private static final string create_table1 = "create table " + table_member + "(" + miembro_id + " integer primary key autoincrement, " + miembro_nombre + " text);"; //table2 private static final string create_table2= "create table " +ejercicios_tabla+"("+ejercicio_id +" integer primary key autoincrement, " +ejercicio_nombre+" text);"; //table3 private static final string create_table3= "create table " +registro_tabla+"(" +id_reg+"integer primary key autoincrement, " +id_u+" integer, " +id_e+" integer, " +rm_max+" real, " +" foreign key (" +id_u+ ") references "+table_member+" ("+ miembro_id +") on delete cascade," +" foreign key ("+ id_e +") references "+ejercicios_tabla+" ("+ ejercicio_id +")on delete cascade;";
and here oncreate() method:
public void oncreate(sqlitedatabase db) { if (!db.isreadonly()) { // enable foreign key constraints db.execsql("pragma foreign_keys=on;"); } db.execsql(create_table1); db.execsql(create_table2); db.execsql(create_table3); db.execsql("insert "+ejercicios_tabla+" ("+ejercicio_nombre+") values('sentadilla') "); db.execsql("insert "+table_member+" ("+miembro_nombre+") values('pedro')");
when create table1 , table2 ok, when create table3 fail.
you're missing closing ) after last cascade.
it should this:
//table3 private static final string create_table3= "create table " +registro_tabla+"(" +id_reg+"integer primary key autoincrement, " +id_u+" integer, " +id_e+" integer, " +rm_max+" real, " +" foreign key (" +id_u+ ") references "+table_member+" ("+ miembro_id +") on delete cascade," +" foreign key ("+ id_e +") references "+ejercicios_tabla+" ("+ ejercicio_id +")on delete cascade);";
Comments
Post a Comment