Java tutorial
/* * Copyright (C) 2014 Lucien Loiseau * This file is part of Rumble. * Rumble is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Rumble is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with Rumble. If not, see <http://www.gnu.org/licenses/>. */ package org.disrupted.rumble.database.statistics; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import org.disrupted.rumble.database.Database; import org.json.JSONArray; import org.json.JSONObject; /** * @author Lucien Loiseau */ public abstract class StatisticDatabase extends Database { public StatisticDatabase(Context context, SQLiteOpenHelper databaseHelper) { super(context, databaseHelper); } public abstract String getTableName(); public JSONArray getJSON() { SQLiteDatabase database = databaseHelper.getReadableDatabase(); Cursor cursor = database.query(getTableName(), null, null, null, null, null, null); if (cursor == null) return null; JSONArray resultSet = new JSONArray(); try { cursor.moveToFirst(); while (cursor.isAfterLast() == false) { int totalColumn = cursor.getColumnCount(); JSONObject rowObject = new JSONObject(); for (int i = 0; i < totalColumn; i++) { if (cursor.getColumnName(i) != null) { try { if (cursor.getString(i) != null) rowObject.put(cursor.getColumnName(i), cursor.getString(i)); else rowObject.put(cursor.getColumnName(i), ""); } catch (Exception e) { } } } resultSet.put(rowObject); cursor.moveToNext(); } cursor.close(); } finally { cursor.close(); } return resultSet; } }