sqlite en Android inserta una nueva fila con algunos valores repetidos

:I have this table:

id            |      name          |      chapter      |    book

 1                  hamlet                  I               Hamlet

 2                  Ismael                  IV              Moby Dick

The behaviour I expect for new values is:

Same name && same book => Character exists so Same id than the existing one:

 2                   Ismael                   X              Moby Dick

Same name && different book => Character doesn't exist so Changes the id:

 3                   Ismael                  XX              The Bible     

Mi pregunta es:

Do I have to make the query before insert new values and then insert the new value?

or there is a way to do it in a automatic way by setting up a trigger or something?

My CREATE TABLE statement

    db.execSQL("CREATE TABLE characters (id INTEGER NOT NULL , 
   name TEXT NOT NULL , chapter TEXT NOT NULL , book TEXT NOT NULL );");

preguntado el 08 de noviembre de 11 a las 12:11

Hi butelo, could you please post your CREATE TABLE statement? -

done, it's quite simple actually -

1 Respuestas

I think the most elegant way of doing so would be for you to isolate the properties of your rows which permit to identify them UNIQUELY. Lets say for instance that name + book make the line unique, then you could concatenate these strings into one identifier in your object, add a column to your database, put and index on it and search on this index when you want to insert new elements.

respondido 08 nov., 11:16

Yeah I'll do it that way. I've just assumed that like we have a constraint for Unique values we could also have ways to manage repeated ones. So there's no way to do it the way I ask? just for documentation pourposes. - butelo

unicity is ensured by primary keys if I'm not mistaken so you could also declare the pair (name, book) as primary key. I might suffice. Something like that : stackoverflow.com/questions/734689/… - Sephy

well actually it would have to be three values: (name, book, chapter) because same character appears in multiple chapters at the same book - butelo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.