Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Store Json Data into Sqlite?

Hi I am creating one app in which i want to send data from mobile to server.but problem is data is reflected on website only when server is online/Up.when server is offline i got response 400 or 502 and so data is displayed on only mobile screen it is not reflected on server.so i want to create Sqlite database so when i take reading data is stored in database and want to upload data which is the server is become online.this jon i want to insert in sqlite.i am new in android so i dont have idea how to insert this json into sqlite.please anyone help me.thanks in advanced

            JSONObject json = new JSONObject();
            json.put("siteId", "");
            if (wtConnected == true) {
                json.put("patientId", 15);
                json.put("deviceMACId", DeviceMac);
                json.put("readingType", 1003);
                json.put("deviceData", weight);
                json.put("deviceType", "WeightScale");

            } else if (bpConnected == true) {
                json.put("patientId", 5);

                json.put("deviceMACId", DeviceMac);
                json.put("readingType", 1002);
                json.put("deviceData", bpdata);
                json.put("deviceType", "BPMonitor");

            } 
            json.put("assetId", "");
            json.put("geoLocationLatitude", "");
            json.put("clientId", "");
            json.put("timeStamp", System.currentTimeMillis());
            json.put("deviceRawData", "");
            json.put("geoLocationLongitude", "");
            System.out.println("Json Is:" + json);
like image 824
user6649633 Avatar asked Oct 30 '22 23:10

user6649633


1 Answers

My friend just do following things :

  1. Define this class which will perform all the operation related to your database creation and manipulation and make changes in this class according to your need.

    DatabaseHandler.java
    
    
    
     public class DatabaseHandler extends SQLiteOpenHelper {
            private static final int DATABASE_VERSION = 1;
            private static final String DATABASE_NAME = "Demo";
            private static final String TABLE_Demo = "demo";
            private static final String KEY_ID = "id";
            private static final String KEY_PATIENT_ID= "patientId";
            private static final String KEY_ReadingType= "readingType";
            private static final String KEY_DeviceMACId= "deviceMACId"; 
            private static final String KEY_DeviceData = "deviceData ";
    
      private static final String KEY_DeviceType= "deviceType";  
            String CREATE_DEMO_TABLE = "CREATE TABLE " + TABLE_Demo +   "("+   
            KEY_ID + " INTEGER PRIMARY KEY,"+ KEY_ReadingType+ " INTEGER ,"  
            +  KEY_DeviceMACId +  "TEXT,"+ KEY_DeviceData + " TEXT,"     
            + KEY_DeviceType +  " TEXT" + ")"; 
    
                public DatabaseHandler(Context context) {
                    super(context, DATABASE_NAME, null, 
    
                }
    
                public void onCreate(SQLiteDatabase db) {
                    db.execSQL(CREATE_DEMO_TABLE);
                }
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                    db.execSQL("DROP TABLE IF EXISTS " + TABLE_Demo );
                    onCreate(db);
                }
    
                void addJson(JsonObject json) {
                    SQLiteDatabase db = this.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put(KEY_PATIENT_ID,   json.getString("patientId")); 
                values.put(KEY_ReadingType,json.getString("readingType"));       
                    values.put(KEY_DeviceMACId, json.getString("deviceMACId")); 
                    values.put(KEY_DeviceData , json.getString("deviceData")); 
                    values.put(KEY_DeviceType, json.getString("deviceType")); 
                    db.insert(TABLE_Demo , null, values);
                    db.close();
                }
    
            }
    
  2. Now make a object of this class and store your json object into database like this:

    DatabaseHandler db = new DatabaseHandler (context); 
         // context of your activity or fragment
        try
        {
        db.addJson(JsonObject json);
    

    // here json is your json object that you need to save in database. } catch(Exception e) { }

like image 184
Dhiraj Choudhary Avatar answered Nov 09 '22 13:11

Dhiraj Choudhary