-1

I have an API from which I receive JSON object response, which is dynamic (meaning response JSON object contains different key-values time to time), so that the database for my Android application need to delete current table and create new table based on JSON keys.

For simple example, today I get JSON object response like

`{"name":"STRING", "age":"INTEGER"}, 

next week I will get response like

{"name":"STRING", "id":"INTEGER","Country":"STRING"}`

I checked Room and Realm, but the problem is that I can't able not get any clear examples or tutorials to make use of it.

FYI - I am new to Android database concept.

Edit #1:

What I need is to create DB Table columns based on JSON keys (i.e, by default table X has two columns A and B and receiving JSON object contains three keys A,B and C, so that DB need to include column C dynamically)

satuser
  • 766
  • 8
  • 17
  • use shared pref for lightweight type, no need to use db. – hemen Apr 19 '19 at 05:21
  • I just gave a simple JSON example, but real DB need to store large data that's why I need to use Database instead of Shared Preferences. – satuser Apr 19 '19 at 05:27
  • so drop the table, create new schema table with columns as per the response – hemen Apr 19 '19 at 05:29
  • is it possible to do it programmatically, please mention how to achieve it in simple words – satuser Apr 19 '19 at 05:31
  • yes, are you new to sqlite? – hemen Apr 19 '19 at 05:33
  • yes, I saw examples about migrations but they we're all adding columns in Entity class and migrating DB to new version. My question is to do it dynamically without need to recode it every time while having new columns. – satuser Apr 19 '19 at 05:46
  • why don't use sql alter command to add new col, https://stackoverflow.com/questions/4253804/insert-new-column-into-table-in-sqlite – hemen Apr 19 '19 at 05:49
  • ok, I can able to create column using alter table for the database but how to dynamically add the column field in model class ? – satuser Apr 19 '19 at 08:34

1 Answers1

0

For storing values in Android Sqlite database check his https://androidjson.com/save-store-php-mysql-json-parsing-data-sqlite/

it will be helpful to you

Khalid Khan
  • 3,017
  • 1
  • 11
  • 27
  • its helpful to know how to load JSON object to database but the problem I'm facing is different. Thanks for the response bro. – satuser Apr 19 '19 at 09:38
  • let me know what you are facing – Khalid Khan Apr 19 '19 at 09:57
  • above link example will store data based on given JSON object values to already defined columns in DB table but what I need is to create Table columns based on JSON keys (i.e, by default table X has two columns A and B and receiving JSON object contains three keys A,B and C, so that DB need to include column C dynamically) – satuser Apr 19 '19 at 11:30