1

I use this Query to get all my elements from TABLE_LISTELEMENT which dont have an entry in the column COL_FK_RID.

db.rawQuery("SELECT * FROM " + TABLE_LISTELEMENT + " WHERE " + COL_FK_RID + " = ?", new String[]{"null"});

Now I check my second table to get the COL_RID

db.rawQuery("SELECT " + COL_RID + " FROM " + TABLE_ROOM + " WHERE "+ COL_RNAME + " = ?",new String[]{room}).toString();

How do I insert the result of my second query in the COL_FK_RID column of my first table?

Rod Kimble
  • 1,302
  • 3
  • 18
  • 44
  • Did you try [this](http://stackoverflow.com/questions/18772803/sql-insert-into-with-subquery-and-value)? – m0skit0 Nov 11 '15 at 11:29
  • have a look here on how to select from table1 row and insert into table2 row -- http://stackoverflow.com/questions/74162/how-to-do-insert-into-a-table-records-extracted-from-another-table – Tasos Nov 11 '15 at 11:30
  • I tried this: `db.insert("listelement(fkrid) SELECT " + COL_RID + " AS " + COL_FK_RID + " FROM " + TABLE_ROOM + " WHERE " + COL_RNAME + " = " + room , null, null);` But the code has no effect (also no error) – Rod Kimble Nov 11 '15 at 12:03

2 Answers2

3

simply use update Query to update the record of the first table but you need a where clause to update it otherwise it will update all the records of the first table.

db.update(TABLE_NAME, contentValues, ATTRIBUTE_ID + " = " + ID, null);

this query will update the table entry and the contentValues are the updated values, if you don't provide any WHERE clause like i have than it will update entire table.

Zubair Akber
  • 2,760
  • 13
  • 30
1

I found the perfect way. This was exactly what I need!

db.execSQL("UPDATE listelement SET fkrid = (SELECT rid FROM room WHERE roomname = '" + roomFromEditText + "')");

Rod Kimble
  • 1,302
  • 3
  • 18
  • 44