It might help if you posted the final string that you're passing to
db.execSQL(), instead of us trying to figure out what the resulting command
is by running the code in our heads.
My guess is that it doesn't work because you're trying to specify a
column-constraint using table-constraint syntax (column constraints don't
use the "FOREIGN KEY" keywords, according to
http://sqlite.org/lang_createtable.html)



On Wed, May 13, 2009 at 1:21 AM, Manu Sánchez <[email protected]>wrote:

> Hi!
> I have create a simple DataBase with two tables Notas and Alumno with them
> ids and one attribute each one, Notas is weak and Alumno is the strong
> table, so I want when I delete an Alumno his nota must be delete too:
>
> Translate: Alumno == Student.                 Notas == Notes
>
> So I use two constraints to do on delete cascade & update like that:
>
> *public* *static* String**
>
> *REFERENCES* = "REFERENCES",
>
> *CONSTRAINT* = "CONSTRAINT",
>
> *FOREIGN_KEY* = "FOREIGN KEY",
>
> *CLAVE_PRIMARIA_GENERICA_TYPE* = "integer primary key autoincrement",
>
> *CREATE_TABLE* = "create table if not exists",
>
> *ESPACIO* = " ";**
>
> *public* *static* *void* creaTablaAlumno (SQLiteDatabase db)
>
>     {
>
>       String DATABASE_CREATE = *CREATE_TABLE*+" Alumno (" +
>
>                   "ID_alumno "+*CLAVE_PRIMARIA_GENERICA_TYPE*+"," +
>
>                   "NombreAlumno text not null"+
>
>                   ");";
>
>       db.execSQL (DATABASE_CREATE);
>
>     }
>
>
>
> *public* *static* *void* creaTablaNota (SQLiteDatabase db)
>
>     {
>
>       String DATABASE_CREATE = *CREATE_TABLE*+" Notas (" +
>
>                   "ID_nota "+*CLAVE_PRIMARIA_GENERICA_TYPE*+"," +
>
>                   "nota integer not null, " +
>
>                   "CD_alumno integer not null," +
>
>                   *constraintFK* ("NOTA_ID_nota","CD_alumno","Alumno",
> "ID_alumno")+
>
>                   ");";
>
>       db.execSQL (DATABASE_CREATE);
>
>     }
>
> *public* *static* String *constraintFK* (String nombreConstraint, String
> columName, String foreignTable, String foreignColumName)
>
> {
>
>             String constraint = *CONSTRAINT*+*ESPACIO*+nombreConstraint+*
> ESPACIO*+*FOREIGN_KEY*+" ("+columName+") "+*REFERENCES*+" "+ foreignTable
> +" ("+foreignColumName+") " +
>
>                         "ON DELETE CASCADE, "+
>
>               *CONSTRAINT*+*ESPACIO*+nombreConstraint+*ESPACIO*+*
> FOREIGN_KEY*+" ("+columName+") "+*REFERENCES*+" "+ foreignTable +" ("
> +foreignColumName+") " + "ON UPDATE CASCADE " ;
>
>                *return* constraint;
>
>         }
>
> *
> **************************************************************************************************************************************************
> *
>
>
>
> After use that code tables are created but *if I delete an Alumno him nota
> won’t be delete* and I don’t know why. My on delete cascade doesn’t work.
>
> ¿Anyone know how to do Foreign key work in Android?
>
> Thanks! J
>
> (Sorry about my English)
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to