mardi 10 novembre 2015

Add FK Value to existing table entry

Ive got some issues with my database on Android.

There are two tables. The first one TABLE_LISTELEMENT handles all the data that one element needs to have.

public static final String SQL_CREATE_LE =
        "create table " + TABLE_LISTELEMENT + "(" +
                COL_LEID + " integer primary key autoincrement, " +
                COL_PRODUCT + " text, " +
                COL_LABEL + " text, " +
                COL_SERIAL + " text, " +
                COL_MAC + " text, " +
                COL_DAAID + " text, " +
                COL_BILL + " text, " +
                COL_FK_RID + " REFERENCES room(rid)" + ")";

Ive added the COL_FK_RID foreign key to reference my second table (TABLE_ROOMS)

public static final String SQL_CREATE_R =
        "create table " + TABLE_ROOM + "(" +
                COL_RID + " integer primary key autoincrement, " +
                COL_RNAME + " text" + ")";

Now Ive got these two tables but I am not shure how to add the FK to my first table.

First I create Elements of TABLE_LISTELEMENTS and show them in a ListView . Now I want to take all of the ListView entries and add a room. Therefore I select all of the entries of TABLE_ELEMENTSwith :

public boolean insertRoomToElement(String fkrid){
    SQLiteDatabase db = this.getWritableDatabase();

    Cursor dbCursor = db.query(TABLE_LISTELEMENT, new String[] {"leid", "product",
                    "label", "serial", "mac", "daaid", "bill"},
            "fkrid " + "", null, null, null, null);

    return true;
}

But how can I now add a room to my TABLE_ROOMS and relate him to all the entries wich I have selectet. (Ive got a EditText and a Button wich I use to enter the room number).

I hope that you understand my problem.

Aucun commentaire:

Enregistrer un commentaire