Rastaman Rastaman - 6 months ago 12
Android Question

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_ELEMENTS
with :

public boolean insertRoomNr(String room){
SQLiteDatabase db = this.getWritableDatabase();
Cursor dbCursor;

ContentValues contentValues = new ContentValues();

contentValues.put("roomname", room);
db.insert(TABLE_ROOM, null, contentValues);
String RaumId ="";
RaumId = db.rawQuery("SELECT rid FROM room WHERE "+ COL_RNAME + " = ?",new String[]{"null"}).toString();

ContentValues contentValues2 = new ContentValues();

contentValues.put("fkrid", RaumId);
db.update(TABLE_LISTELEMENT, contentValues2 , " WHERE fkrid IS 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.

Answer

You could also use something like:

public boolean setData(String data1, String data2) {

    SQLiteDatabase database = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();

    contentValues.put("data_example1", data1);
    contentValues.put("data_example2", data2);

    db.insert(TABLE_EXAMPLE, null, contentValues);

    return true;
}
Comments