I'm trying to get id
column from my database, ad it to ArrayList and to each id add "\t0",
My database is created using Room, i have a lot of column which one of them is
@PrimaryKey(autoGenerate = true)
private int id;
I am operating using ItemDAO
and i have there function
@Query("SELECT * FROM item")
List<Item> getItems();
Which writes to ArrayList<Items>
all of contents
I was thinking of running it trough the loop getting id
and adding to ArrayList<String>
but this doesn't seems to be eficient.
android.arch.persistence.room.Dao. Marks the class as a Data Access Object. Data Access Objects are the main classes where you define your database interactions. They can include a variety of query methods. The class marked with @Dao should either be an interface or an abstract class.
Now, let’s move towards the implementation of Room Database in Android. To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language. Navigate to the app > Gradle Scripts > build.gradle file and add the below dependencies in the dependencies section.
In this tutorial, you'll get started with Android room. Room is an ORM or abstraction on top of SQLite database. It's part of the Architecture Components by Google. Room enables you to easily work SQLite databases in Android.
Room’s dependencies are available via Google’s new Maven repository, simply add it to the list of repositories in your main build.gradle file: Define your Room library version in the same file. For now, it’s in alpha, but keep an eye on our developer pages for version updates. ... In your app/build.gradle file, add the dependencies for Room.
• By default, Room uses the field names as the column names in the database. If you want a column to have a different name, add the @ColumnInfo annotation to a field. Basically a container of exercises that together create an exercise routine.
Your DAO:
@Query("SELECT Id FROM item")
List<Integer> getAllIds();
Your model:
@ColumnInfo(name = "Id")
@PrimaryKey(autoGenerate = true)
private int id;
In you query SELECT * FROM item
* means select All, put there your column name and you will get list of objects from that column
Example: Select all items in id column SELECT id FROM item
I tried to modify and test @Valgaal 's solution. It turns out that Room can also return other type of values, more than just id (or integer).
For example, you can write an item class like this:
@Entity(tableName = Item.TABLE_NAME)
public class Item {
public static final String TABLE_NAME = "ItemsTable";
public static final String COL_DESC = "Description";
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = COL_DESC)
private String description;
// getter & setter...
}
And then, you can write Dao like this:
@Dao
public interface ItemDao {
@Query("SELECT * FROM " + Item.TABLE_NAME)
List<Item> getItems();
@Query("SELECT " + Item.COL_DESC + " FROM " + Item.TABLE_NAME)
List<String> getItemDescriptions();
}
And it's functional as it should be. I guess all of the other data types that Room can save (including custom types?) can be queried (and returned lists of specific column data) by the same logic above. Hope this would help someone in the future!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With