public static JSONArray storeInDB(SQLiteManager dbHelper, JSONObject jsonobj) { // Gets the data repository in write mode SQLiteDatabase db = dbHelper.getWritableDatabase(); dbHelper.onCreate(db); try { JSONArray jsonarray = jsonobj.getJSONArray("features"); Log.d("EarthquakeMonitor", "MainActivity.storeInDB() deleted old records"); dbHelper.deleteRecords(db); Log.d("EarthquakeMonitor", "MainActivity.storeInDB() length is => " + jsonarray.length()); for (int i = 0; i < jsonarray.length(); i++) { JSONObject row = jsonarray.getJSONObject(i); // Log.d("EarthquakeMonitor", "** "+row.getString("id")+", // "+row.getJSONObject("properties").getDouble("mag")+", // "+Helpers.getDate(row.getJSONObject("properties").getInt("time"))+", // "+row.getJSONObject("properties").getString("place")); // Create a new map of values, where column names are the keys ContentValues values = new ContentValues(); values.put(FeedEntry.COLUMN_NAME_ID, row.getString("id")); values.put(FeedEntry.COLUMN_NAME_MAG, row.getJSONObject("properties").getDouble("mag")); values.put(FeedEntry.COLUMN_NAME_PLACE, row.getJSONObject("properties").getString("place")); values.put(FeedEntry.COLUMN_NAME_TIME, row.getJSONObject("properties").getLong("time")); values.put( FeedEntry.COLUMN_NAME_LAT, Helpers.stringtoArray(row.getJSONObject("geometry").getString("coordinates"))[1]); values.put( FeedEntry.COLUMN_NAME_LNG, Helpers.stringtoArray(row.getJSONObject("geometry").getString("coordinates"))[0]); values.put( FeedEntry.COLUMN_NAME_DPT, Helpers.stringtoArray(row.getJSONObject("geometry").getString("coordinates"))[2]); // Insert the new row, returning the primary key value of the new row long newRowId; newRowId = db.insert(FeedEntry.TABLE_NAME, FeedEntry.COLUMN_NAME_PLACE, values); } Log.d( "EarthquakeMonitor", "MainActivity.storeInDB() jsonarray length stored = > " + jsonarray.length()); return jsonarray; } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); return null; } }
public static JSONArray findEntryById(SQLiteManager dbHelper, String id_feed) { SQLiteDatabase db = dbHelper.getReadableDatabase(); Log.d("EarthquakeMonitor", "SQLiteManager findEntryById() id_feed is " + id_feed); // Define a projection that specifies which columns from the database // you will actually use after this query. String[] projection = { FeedEntry.COLUMN_NAME_ID, FeedEntry.COLUMN_NAME_MAG, FeedEntry.COLUMN_NAME_PLACE, FeedEntry.COLUMN_NAME_TIME, FeedEntry.COLUMN_NAME_LAT, FeedEntry.COLUMN_NAME_LNG, FeedEntry.COLUMN_NAME_DPT }; // How you want the results sorted in the resulting Cursor String sortOrder = "time DESC"; Cursor c = db.rawQuery( "SELECT * FROM " + FeedEntry.TABLE_NAME + " WHERE id_feed='" + id_feed + "'", null); /*Cursor c = db.query( FeedEntry.TABLE_NAME, // The table to query projection, // The columns to return "id_feed=", // The columns for the WHERE clause new String[] {id_feed}, // The values for the WHERE clause null, // don't group the rows null, // don't filter by row groups sortOrder // The sort order );*/ JSONArray resultSet = new JSONArray(); JSONObject returnObj = new JSONObject(); c.moveToFirst(); while (c.isAfterLast() == false) { int totalColumn = c.getColumnCount(); JSONObject rowObject = new JSONObject(); for (int i = 0; i < totalColumn; i++) { if (c.getColumnName(i) != null) { try { if (c.getString(i) != null) { Log.d("TAG_NAME", c.getString(i)); rowObject.put(c.getColumnName(i), c.getString(i)); } else { rowObject.put(c.getColumnName(i), ""); } } catch (Exception e) { Log.d("TAG_NAME", e.getMessage()); } } } resultSet.put(rowObject); c.moveToNext(); } c.close(); Log.d( "EarthquakeMonitor", "MainActivity.findEntryById() resultSet length = > " + resultSet.length()); Log.d( "EarthquakeMonitor", "MainActivity.findEntryById() resultSet = > " + resultSet.toString()); return resultSet; }