There are two tables in the database. The first table contains 3 columns:

| ID | ItemTitle | ItemContent |

The second table has 2 columns:

| ID | Favorite |

I need to display the data of the first table by indexes of the Favorite second table, where the Favorite is of the NUMERIC type.

 Cursor cursor = sqLiteDatabase.query(MainItemTable.mainItemTable, MainItemTable.columnsMainItem, // тут нужно обратиться к индексу Favorite другой таблицы // например, пытался сделать так: FavoriteTable.favorite + " = 1", null, null, null, null); 

Is it possible? I suppose that even if I may need to do rawQuery ?

  • possible and query and rawquery stackoverflow.com/a/34688420/9271527 - Andrey Mihalev
  • What is the relationship between the tables? I ’ve written a few words about my favorites before , in general, then two tables are not needed here, this will only complicate the selection - pavlofff
  • @pavlofff has a lot of tables, there is a table with the main contents, there is a table of categories, and there can be a lot of categories, all tables have the ability to add items to favorites, and I decided to use one table for favorites indexes, I did not find a more thoughtful method)) and the idea arose to make a selection from another table. - McDaggen
  • In principle, @pavlofff still had the idea to make the favorite table similar to the main table, and if the user adds to the favorites, this item is copied to the favorite table, but so far he hasn’t done so, since each item can still be edited, and then after the edit you need will update it in the current item and in the favorite table - McDaggen
  • If the Favorite ID table matches the Favorite Record ID in the Content Table, then the query will be: SELECT ItemTitle, ItemContent FROM TableContent, TableFavorite WHERE TableFavorite.ID = TableContent.ID . to broaden horizons, you can (and should) read about communications in SQL — one to many, many to many. The second thought is absolutely wrong :) - pavlofff

1 answer 1

Everything turned out to be much easier:

 Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM Main_item_table, Favorite_table WHERE favorite = 1", null); 
  • one
    but since you don’t have any connection between the tables, when you delete some and then add other records, the order will be broken and everything will "float". sql-databases do not guarantee the order of elements, there must be an explicit connection, for example, by id, so you must either postpone other books and read on sql (I can advise “learn sql” by L. Bailey - very accessible), or postpone this task - pavlofff
  • @pavlofff Perhaps I will postpone other books) - McDaggen