0

I am trying to pre-populate a room database with some data using DB Browser for sqlite.

I have a table defined with (name: String, score:Int, image:Int). That image field stores an image from drawable folder so the example for an insert would be :

INSERT INTO player (name,score,image) VALUES ("Mike", 31, R.drawable.mike)

Since the table field is Int, DB Browser wont let me store R.drawable.mike which is the image location in android studio as it expects a number. Error :

Execution finished with errors.
Result: no such column: R.drawable.mike
At line 1:
INSERT INTO player (name,score,image) VALUES ("Mike", 31, R.drawable.mike)

I have tried using a BLOB type for image but it returns the same error as above.

Any ideas on how I can do this ? If I define image to be a string how can I make android studio read "R.drawable.mike" as id location later in the code?

Or how can I prepopulate a room database with such data in some other way ?

  • I am considering using a string anyway and then converting it later like this (but in kotlin) : String name = "your_drawable"; int id = getResources().getIdentifier(name, "drawable", getPackageName()); Drawable drawable = getResources().getDrawable(id); – Grim Reaper Jul 11 '22 at 02:03
  • Clearly this piece of data is a string: `R.drawable.mike`. I'm not familiar with android studio etc. Does it at some point map to an int? That must occur in your application code somewhere. – Nick.Mc Jul 11 '22 at 02:17
  • I believe that at runtime it maps to the location of the image in the resources folder. Android studio considers that to be an int as it points to a location in the phone's memory. But the DB browser does not have such structure/system. – Grim Reaper Jul 11 '22 at 03:12
  • Perhaps you should post the bit of code that is meant to utilise this piece of data. – Nick.Mc Jul 11 '22 at 03:38

0 Answers0