Example usage for android.database.sqlite SQLiteDatabase close

List of usage examples for android.database.sqlite SQLiteDatabase close

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase close.

Prototype

public void close() 

Source Link

Document

Releases a reference to the object, closing the object if the last reference was released.

Usage

From source file:com.rener.sea.DBHelper.java

private long setAddress(JSONArray data) {
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;/*from w  w  w . j  av  a 2s . c  o m*/
    try {
        for (i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);

            ContentValues values = new ContentValues();
            if (!item.isNull(DBSchema.ADDRESS_ID))
                values.put(DBSchema.ADDRESS_ID, item.getLong(DBSchema.ADDRESS_ID));
            if (!item.isNull(DBSchema.ADDRESS_LINE1))
                values.put(DBSchema.ADDRESS_LINE1, item.getString(DBSchema.ADDRESS_LINE1));
            if (!item.isNull(DBSchema.ADDRESS_CITY))
                values.put(DBSchema.ADDRESS_CITY, item.getString(DBSchema.ADDRESS_CITY));
            if (!item.isNull(DBSchema.ADDRESS_ZIPCODE))
                values.put(DBSchema.ADDRESS_ZIPCODE, item.getString(DBSchema.ADDRESS_ZIPCODE));
            if (!item.isNull(DBSchema.ADDRESS_LINE2))
                values.put(DBSchema.ADDRESS_LINE2, item.getString(DBSchema.ADDRESS_LINE2));
            if (!item.isNull(DBSchema.STATUS))
                values.put(DBSchema.STATUS, item.getString(DBSchema.STATUS));
            values.put(DBSchema.MODIFIED, DBSchema.MODIFIED_NO);

            db.insertWithOnConflict(DBSchema.TABLE_ADDRESS, null, values, 5);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.close();
    return i;
}

From source file:com.rener.sea.DBHelper.java

private long setUsers(JSONArray data) {
    //        Log.i(this.toString(), "USERS : " + data);
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;/*w w  w. j  ava 2  s  .c  o m*/
    try {
        for (i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);

            ContentValues values = new ContentValues();
            if (!item.isNull(DBSchema.USER_ID))
                values.put(DBSchema.USER_ID, item.getLong(DBSchema.USER_ID));
            if (!item.isNull(DBSchema.USER_USERNAME))
                values.put(DBSchema.USER_USERNAME, item.getString(DBSchema.USER_USERNAME));
            if (!item.isNull(DBSchema.USER_PASSHASH))
                values.put(DBSchema.USER_PASSHASH, item.getString(DBSchema.USER_PASSHASH));
            if (!item.isNull(DBSchema.USER_PERSON_ID))
                values.put(DBSchema.USER_PERSON_ID, item.getLong(DBSchema.USER_PERSON_ID));
            if (!item.isNull(DBSchema.USER_SALT))
                values.put(DBSchema.USER_SALT, item.getString(DBSchema.USER_SALT));
            if (!item.isNull(DBSchema.USER_TYPE))
                values.put(DBSchema.USER_TYPE, item.getString(DBSchema.USER_TYPE));
            if (!item.isNull(DBSchema.STATUS))
                values.put(DBSchema.STATUS, item.getString(DBSchema.STATUS));
            values.put(DBSchema.MODIFIED, DBSchema.MODIFIED_NO);

            db.insertWithOnConflict(DBSchema.TABLE_USERS, null, values, 5);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.close();
    return i;
}

From source file:org.cgiar.ilri.odk.pull.backend.services.FetchFormDataService.java

/**
 * Dumps data provided the rows variable into the specified database. The location of the database
 * is in the form's media folder in ODK's SDCard's folder.
 *
 * Indexes in {@param rows} are expected to correspond to rows in {@param org.cgiar.ilri.odk.pull.backend.carriers.Form.DB_DATA_TABLE} for {@param fileName}.
 * Each JSONArray element should be a JSONObject with children being column values (with keys being column names).
 * Make sure all JSONObjects in the JSONArray have the same number of key-value pairs.
 *
 * @param fileName  Then name to be given to the Database (without the .db suffix)
 * @param rows      The {@link org.json.JSONArray} object containing the data
 * @return  TRUE if database created successfully
 *//*  w w  w.  j ava2s  .c  o m*/
private boolean saveDataInDb(String fileName, JSONArray rows) {
    boolean result = false;
    //TODO: only do this if ODK Collect is not using this file
    String pathToFile = Form.BASE_ODK_LOCATION + formName + Form.EXTERNAL_ITEM_SET_SUFFIX;
    /*File existingDb = new File(pathToFile+File.separator+fileName+Form.SUFFIX_DB);
    existingDb.delete();*/
    final DatabaseHelper databaseHelper = new DatabaseHelper(this, fileName, 1, pathToFile);
    SQLiteDatabase db = null;
    try {
        db = databaseHelper.getWritableDatabase();
    } catch (SQLiteException e) {//probably because the existing .db file is corrupt
        e.printStackTrace();
        Log.w(TAG, "Unable to open database in " + pathToFile + File.separator + fileName + Form.SUFFIX_DB
                + " most likely because the database is corrupt. Trying to recreate db file");
        File existingDbFile = new File(pathToFile + File.separator + fileName + Form.SUFFIX_DB);
        existingDbFile.delete();
        File existingDbJournalFile = new File(
                pathToFile + File.separator + fileName + Form.SUFFIX_DB + Form.SUFFIX_JOURNAL);
        existingDbJournalFile.delete();
        try {
            db = databaseHelper.getWritableDatabase();
        } catch (SQLiteException e1) {
            Log.e(TAG,
                    "Unable to recreate " + pathToFile + File.separator + fileName + Form.SUFFIX_DB + "  file");
            e1.printStackTrace();
        }
    }
    if (rows.length() > 0 && db != null) {
        try {
            List<String> columns = new ArrayList<String>();
            List<String> indexes = new ArrayList<String>();
            Iterator<String> iterator = rows.getJSONObject(0).keys();
            //recreate the tables
            db.execSQL("drop table if exists " + Form.DB_METADATA_TABLE);
            String createMetaTableString = "create table " + Form.DB_METADATA_TABLE + " ("
                    + Form.DB_META_LOCALE_FIELD + " " + Form.DB_META_LOCALE_FIELD_TYPE + ")";
            db.execSQL(createMetaTableString);
            databaseHelper.runInsertQuery(Form.DB_METADATA_TABLE, new String[] { Form.DB_META_LOCALE_FIELD },
                    new String[] { Form.DB_DEFAULT_LOCALE }, -1, db);
            db.execSQL("drop table if exists " + Form.DB_DATA_TABLE);
            String createTableString = "create table " + Form.DB_DATA_TABLE + " (";
            while (iterator.hasNext()) {
                String currKey = iterator.next();
                if (columns.size() > 0) {//this is the first column
                    createTableString = createTableString + ", ";
                }
                createTableString = createTableString + Form.DB_DATA_COLUMN_PREFIX + currKey + " "
                        + Form.DB_DATA_COLUMN_TYPE;
                columns.add(currKey);
                if (currKey.endsWith(Form.SUFFIX_INDEX_FIELD)) {
                    Log.d(TAG, fileName + " has an index column " + currKey);
                    indexes.add(currKey);
                }
            }
            //only continue if we have at least one column
            if (columns.size() > 0) {
                createTableString = createTableString + ", " + Form.DB_DATA_SORT_FIELD + " "
                        + Form.DB_DATA_SORT_COLUMN_TYPE + ")";
                db.execSQL(createTableString);
                for (int index = 0; index < indexes.size(); index++) {
                    db.execSQL("create index " + indexes.get(index) + Form.SUFFIX_INDEX + " on "
                            + Form.DB_DATA_TABLE + "(" + Form.DB_DATA_COLUMN_PREFIX + indexes.get(index) + ")");
                }
                for (int rowIndex = 0; rowIndex < rows.length(); rowIndex++) {
                    JSONObject currRow = rows.getJSONObject(rowIndex);
                    String[] currColumns = new String[columns.size() + 1];
                    String[] currValues = new String[columns.size() + 1];
                    for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
                        currColumns[columnIndex] = Form.DB_DATA_COLUMN_PREFIX + columns.get(columnIndex);
                        currValues[columnIndex] = currRow.getString(columns.get(columnIndex));
                    }
                    currColumns[columns.size()] = Form.DB_DATA_SORT_FIELD;
                    currValues[columns.size()] = String.valueOf((double) rowIndex);//TODO: not sure if should be float or double
                    databaseHelper.runInsertQuery(Form.DB_DATA_TABLE, currColumns, currValues, -1, db);//do not add unique key field index in argument list. Will end up being an extra query
                }
                result = true;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Log.w(TAG, "Provided jsonArray to be dumped into a db is empty");
    }
    db.close();
    //copy db to the ADB push directory
    File adbFormDir = new File(
            Form.BASE_ODK_LOCATION + formName.replaceAll("[^A-Za-z0-9]", "_") + Form.EXTERNAL_ITEM_SET_SUFFIX);
    if (!adbFormDir.exists() || !adbFormDir.isDirectory()) {
        adbFormDir.setWritable(true);
        adbFormDir.setReadable(true);
        Log.i(TAG, "Trying to create dir " + adbFormDir.getPath());
    }
    File sourceDbFile = new File(pathToFile + File.separator + fileName + Form.SUFFIX_DB);
    File destDbFile = new File(Form.BASE_ODK_LOCATION + formName.replaceAll("[^A-Za-z0-9]", "_")
            + Form.EXTERNAL_ITEM_SET_SUFFIX + File.separator + fileName + Form.SUFFIX_DB);
    InputStream in = null;
    OutputStream out = null;
    try {
        in = new FileInputStream(sourceDbFile);
        out = new FileOutputStream(destDbFile);
        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.snt.bt.recon.database.DBHandler.java

/**
 * Get all from a table and return a list
 * @return// w  w w .j  av  a2 s  .  com
 */
public <T> List<T> getAll(Class<T> cl) throws InstantiationException, IllegalAccessException {
    T inst = cl.newInstance();
    List<T> list = new ArrayList<T>();

    // Select All Query
    String selectQuery;
    Cursor cursor;
    SQLiteDatabase db = this.getWritableDatabase();
    if (inst instanceof Trip) {
        // Select All Query
        selectQuery = "SELECT * FROM " + TABLE_TRIPS;
        cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Trip trip = new Trip();
                trip.setSessionId(UUID.fromString(cursor.getString(0)));
                trip.setImei(cursor.getString(1));
                trip.setTransport(cursor.getString(2));

                trip.setTimestampStart(cursor.getString(3));
                trip.setTimestampEnd(cursor.getString(4));
                trip.setAppVersion(cursor.getString(5));
                trip.setUploadStatus(cursor.getString(6));
                // Adding contact to list
                list.add((T) trip);
            } while (cursor.moveToNext());
        }
        cursor.close();

    } else if (inst instanceof GPSLocation) {
        // Select All Query
        selectQuery = "SELECT * FROM " + TABLE_LOCATIONS;
        cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                GPSLocation location = new GPSLocation();
                location.setLocationId(UUID.fromString(cursor.getString(0)));
                location.setSessionId(UUID.fromString(cursor.getString(1)));
                location.setTimestamp(cursor.getString(2));
                location.setLatitude(cursor.getFloat(3));
                location.setLongitude(cursor.getFloat(4));
                location.setSpeed(cursor.getFloat(5));
                location.setBearing(cursor.getFloat(6));
                location.setAltitude(cursor.getFloat(7));
                location.setAccuracy(cursor.getFloat(8));
                location.setUploadStatus(cursor.getString(9));

                // Adding contact to list
                list.add((T) location);
            } while (cursor.moveToNext());
        }
        cursor.close();

    } else if (inst instanceof BluetoothClassicEntry) {
        // Select All Query
        selectQuery = "SELECT * FROM " + TABLE_BC;
        cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                BluetoothClassicEntry bc_entry = new BluetoothClassicEntry();
                bc_entry.setSessionId(UUID.fromString(cursor.getString(1)));//START FROM 1 SINCE 0 is ID
                bc_entry.setLocationId(UUID.fromString(cursor.getString(2)));
                bc_entry.setTimestamp(cursor.getString(3));
                bc_entry.setMac(cursor.getString(4));
                bc_entry.setType(cursor.getInt(5));
                bc_entry.setRssi(cursor.getInt(6));
                bc_entry.setDeviceName(cursor.getString(7));
                bc_entry.setBcClass(cursor.getString(8));
                bc_entry.setUploadStatus(cursor.getString(9));

                // Adding contact to list
                list.add((T) bc_entry);
            } while (cursor.moveToNext());
        }
        cursor.close();

    } else if (inst instanceof BluetoothLowEnergyEntry) {
        // Select All Query
        selectQuery = "SELECT * FROM " + TABLE_BLE;
        cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                BluetoothLowEnergyEntry ble_entry = new BluetoothLowEnergyEntry();
                ble_entry.setSessionId(UUID.fromString(cursor.getString(1)));//START FROM 1 SINCE 0 is ID
                ble_entry.setLocationId(UUID.fromString(cursor.getString(2)));
                ble_entry.setTimestamp(cursor.getString(3));
                ble_entry.setMac(cursor.getString(4));
                ble_entry.setRssi(cursor.getInt(5));
                ble_entry.setDeviceName(cursor.getString(6));
                ble_entry.setBleAdvData(cursor.getString(7));
                ble_entry.setUploadStatus(cursor.getString(8));

                // Adding contact to list
                list.add((T) ble_entry);
            } while (cursor.moveToNext());
        }
        cursor.close();

    }

    db.close();

    // return  list
    return list;
}

From source file:com.rener.sea.DBHelper.java

private long setFlowchart(JSONArray data) {
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;//w  w w.  j  a  va  2  s.c  om
    try {
        for (i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);

            ContentValues values = new ContentValues();
            if (!item.isNull(DBSchema.FLOWCHART_ID))
                values.put(DBSchema.FLOWCHART_ID, item.getLong(DBSchema.FLOWCHART_ID));
            if (!item.isNull(DBSchema.FLOWCHART_FIRST_ID))
                values.put(DBSchema.FLOWCHART_FIRST_ID, item.getString(DBSchema.FLOWCHART_FIRST_ID));
            if (!item.isNull(DBSchema.FLOWCHART_NAME))
                values.put(DBSchema.FLOWCHART_NAME, item.getString(DBSchema.FLOWCHART_NAME));
            if (!item.isNull(DBSchema.FLOWCHART_END_ID))
                values.put(DBSchema.FLOWCHART_END_ID, item.getLong(DBSchema.FLOWCHART_END_ID));
            if (!item.isNull(DBSchema.FLOWCHART_CREATOR_ID))
                values.put(DBSchema.FLOWCHART_CREATOR_ID, item.getString(DBSchema.FLOWCHART_CREATOR_ID));
            if (!item.isNull(DBSchema.FLOWCHART_VERSION))
                values.put(DBSchema.FLOWCHART_VERSION, item.getString(DBSchema.FLOWCHART_VERSION));
            if (!item.isNull(DBSchema.STATUS))
                values.put(DBSchema.STATUS, item.getString(DBSchema.STATUS));
            values.put(DBSchema.MODIFIED, DBSchema.MODIFIED_NO);

            db.insertWithOnConflict(DBSchema.TABLE_FLOWCHART, null, values, 5);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.close();
    return i;
}

From source file:com.rener.sea.DBHelper.java

public List<Person> getAllPersons() {
    SQLiteDatabase db = getReadableDatabase();
    //        Cursor cursor = db.query(DBSchema.TABLE_PERSON, new String[]{DBSchema.PERSON_ID},
    //                null, null, null, null, DBSchema.PERSON_FIRST_NAME + " COLLATE NOCASE", null);
    Cursor cursor = db.rawQuery("SELECT " + DBSchema.PERSON_ID + ", " + DBSchema.PERSON_FIRST_NAME + ", "
            + DBSchema.PERSON_MIDDLE_INITIAL + ", " + DBSchema.PERSON_LAST_NAME1 + ", "
            + DBSchema.PERSON_LAST_NAME2 + " " + "FROM " + DBSchema.TABLE_PERSON + " WHERE " + DBSchema.STATUS
            + " !=? AND " + DBSchema.PERSON_ID + " NOT IN (SELECT " + DBSchema.USER_PERSON_ID + " FROM "
            + DBSchema.TABLE_USERS + ") " + "ORDER BY " + DBSchema.PERSON_FIRST_NAME + " COLLATE NOCASE",
            new String[] { String.valueOf(-1) });
    ArrayList<Person> persons;
    persons = new ArrayList<>();
    //        Log.i(this.toString(), "Cursor " + cursor);
    //        Log.i(this.toString(), "Cursor count " + cursor.getCount());
    if ((cursor != null) && (cursor.getCount() > 0)) {
        //            Log.i(this.toString(), "Inside if");
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            persons.add(new Person(cursor.getLong(0), cursor.getString(1),
                    (cursor.isNull(2) ? "" : cursor.getString(2)), cursor.getString(3),
                    (cursor.isNull(4) ? "" : cursor.getString(4)), this));
            //                Log.i(this.toString(), "People created " + cursor.getLong(0));
        }/*from   ww w  .j  a  v  a2  s. c o  m*/

        db.close();
        cursor.close();

    }
    //        Log.i(this.toString(), "persons not found");
    return persons;

}

From source file:com.rener.sea.DBHelper.java

private long setAppointments(JSONArray data) {
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;//ww  w.j  a va 2 s  .  c  o  m
    try {
        for (i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);

            ContentValues values = new ContentValues();
            if (!item.isNull(DBSchema.APPOINTMENT_ID))
                values.put(DBSchema.APPOINTMENT_ID, item.getLong(DBSchema.APPOINTMENT_ID));
            if (!item.isNull(DBSchema.APPOINTMENT_DATE))
                values.put(DBSchema.APPOINTMENT_DATE, item.getString(DBSchema.APPOINTMENT_DATE));
            if (!item.isNull(DBSchema.APPOINTMENT_TIME))
                values.put(DBSchema.APPOINTMENT_TIME, item.getString(DBSchema.APPOINTMENT_TIME));
            if (!item.isNull(DBSchema.APPOINTMENT_REPORT_ID))
                values.put(DBSchema.APPOINTMENT_REPORT_ID, item.getLong(DBSchema.APPOINTMENT_REPORT_ID));
            if (!item.isNull(DBSchema.APPOINTMENT_PURPOSE))
                values.put(DBSchema.APPOINTMENT_PURPOSE, item.getString(DBSchema.APPOINTMENT_PURPOSE));
            if (!item.isNull(DBSchema.APPOINTMENT_MAKER_ID))
                values.put(DBSchema.APPOINTMENT_MAKER_ID, item.getLong(DBSchema.APPOINTMENT_MAKER_ID));
            if (!item.isNull(DBSchema.STATUS))
                values.put(DBSchema.STATUS, item.getString(DBSchema.STATUS));
            values.put(DBSchema.MODIFIED, DBSchema.MODIFIED_NO);

            db.insertWithOnConflict(DBSchema.TABLE_APPOINTMENTS, null, values, 5);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.close();
    return i;
}

From source file:com.snt.bt.recon.database.DBHandler.java

/**
 * Compose JSON out of SQLite records//from   w  w  w  .  j ava  2s.c  om
 * @return
 */
public <T> String composeJSONfromSQLite(Class<T> cl) throws InstantiationException, IllegalAccessException {
    T inst = cl.newInstance();
    List<T> list = new ArrayList<T>();

    // Select All Query
    String selectQuery;
    Cursor cursor;
    SQLiteDatabase db = this.getWritableDatabase();
    if (inst instanceof Trip) {
        // Select All Query
        selectQuery = "SELECT * FROM " + TABLE_TRIPS + " where " + KEY_UPLOAD_STATUS + " = '" + "no" + "' or "
                + KEY_UPLOAD_STATUS + " = '" + "partial" + "'";
        cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Trip trip = new Trip();
                trip.setSessionId(UUID.fromString(cursor.getString(0)));
                trip.setImei(cursor.getString(1));
                trip.setTransport(cursor.getString(2));

                trip.setTimestampStart(cursor.getString(3));
                trip.setTimestampEnd(cursor.getString(4));
                trip.setAppVersion(cursor.getString(5));

                // Adding contact to list
                list.add((T) trip);
            } while (cursor.moveToNext());
        }
        cursor.close();

    } else if (inst instanceof GPSLocation) {
        // Select All Query
        selectQuery = "SELECT * FROM " + TABLE_LOCATIONS + " where " + KEY_UPLOAD_STATUS + " = '" + "no" + "'";
        cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                GPSLocation location = new GPSLocation();
                location.setLocationId(UUID.fromString(cursor.getString(0)));
                location.setSessionId(UUID.fromString(cursor.getString(1)));
                location.setTimestamp(cursor.getString(2));
                location.setLatitude(cursor.getFloat(3));
                location.setLongitude(cursor.getFloat(4));
                location.setSpeed(cursor.getFloat(5));
                location.setBearing(cursor.getFloat(6));
                location.setAltitude(cursor.getFloat(7));
                location.setAccuracy(cursor.getFloat(8));

                // Adding contact to list
                list.add((T) location);
            } while (cursor.moveToNext());
        }
        cursor.close();

    }

    else if (inst instanceof BluetoothClassicEntry) {
        // Select All Query
        selectQuery = "SELECT * FROM " + TABLE_BC + " where " + KEY_UPLOAD_STATUS + " = '" + "no" + "'";
        cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                BluetoothClassicEntry bc_entry = new BluetoothClassicEntry();
                bc_entry.setId(cursor.getInt(0));
                bc_entry.setSessionId(UUID.fromString(cursor.getString(1)));
                bc_entry.setLocationId(UUID.fromString(cursor.getString(2)));
                bc_entry.setTimestamp(cursor.getString(3));
                bc_entry.setMac(cursor.getString(4));
                bc_entry.setType(cursor.getInt(5));
                bc_entry.setRssi(cursor.getInt(6));
                bc_entry.setDeviceName(cursor.getString(7));
                bc_entry.setBcClass(cursor.getString(8));

                // Adding contact to list
                list.add((T) bc_entry);
            } while (cursor.moveToNext());
        }
        cursor.close();

    }

    else if (inst instanceof BluetoothLowEnergyEntry) {
        // Select All Query
        selectQuery = "SELECT * FROM " + TABLE_BLE + " where " + KEY_UPLOAD_STATUS + " = '" + "no" + "'";
        cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                BluetoothLowEnergyEntry ble_entry = new BluetoothLowEnergyEntry();
                ble_entry.setId(cursor.getInt(0));
                ble_entry.setSessionId(UUID.fromString(cursor.getString(1)));
                ble_entry.setLocationId(UUID.fromString(cursor.getString(2)));
                ble_entry.setTimestamp(cursor.getString(3));
                ble_entry.setMac(cursor.getString(4));
                ble_entry.setRssi(cursor.getInt(5));
                ble_entry.setDeviceName(cursor.getString(6));
                ble_entry.setBleAdvData(cursor.getString(7));

                // Adding contact to list
                list.add((T) ble_entry);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }

    db.close();

    // return contact list
    Gson gson = new GsonBuilder().create();
    //Use GSON to serialize Array List to JSON
    Log.d("DatabaseTest", "composeJSONfromSQLite " + cl.getName() + " " + gson.toJson(list));

    return gson.toJson(list);
}

From source file:com.rener.sea.DBHelper.java

private JSONArray getItem() {

    JSONArray data;//from  w w w .j a v a  2  s  . c o  m
    data = new JSONArray();
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_ITEM,
            new String[] { DBSchema.ITEM_ID, DBSchema.ITEM_FLOWCHART_ID, DBSchema.ITEM_LABEL,
                    DBSchema.ITEM_TYPE },
            DBSchema.MODIFIED + "=?", new String[] { DBSchema.MODIFIED_YES }, null, null, null, null);
    if (cursor.moveToFirst()) {
        if ((cursor != null) && (cursor.getCount() > 0))
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                JSONObject map = new JSONObject();
                try {
                    if (!cursor.isNull(0))
                        map.put(DBSchema.ITEM_ID, cursor.getString(0));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    if (!cursor.isNull(1))
                        map.put(DBSchema.ITEM_FLOWCHART_ID, cursor.getString(1));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    if (!cursor.isNull(2))
                        map.put(DBSchema.ITEM_LABEL, cursor.getString(2));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    if (!cursor.isNull(3))
                        map.put(DBSchema.ITEM_TYPE, cursor.getString(3));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                data.put(map);
            }
    }
    db.close();
    cursor.close();
    return data;

}

From source file:com.rener.sea.DBHelper.java

private JSONArray getPath() {

    JSONArray data;//from w  ww . j  a va  2  s.c  o  m
    data = new JSONArray();
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_PATH,
            new String[] { DBSchema.PATH_REPORT_ID, DBSchema.PATH_OPTION_ID, DBSchema.PATH_DATA,
                    DBSchema.PATH_SEQUENCE },
            DBSchema.MODIFIED + "=?", new String[] { DBSchema.MODIFIED_YES }, null, null, null, null);
    if (cursor.moveToFirst()) {
        if ((cursor != null) && (cursor.getCount() > 0))
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                JSONObject map = new JSONObject();
                try {
                    if (!cursor.isNull(0))
                        map.put(DBSchema.PATH_REPORT_ID, cursor.getString(0));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    if (!cursor.isNull(1))
                        map.put(DBSchema.PATH_OPTION_ID, cursor.getString(1));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    if (!cursor.isNull(2))
                        map.put(DBSchema.PATH_DATA, cursor.getString(2));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    if (!cursor.isNull(3))
                        map.put(DBSchema.PATH_SEQUENCE, cursor.getString(3));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                data.put(map);
            }
    }
    db.close();
    cursor.close();
    return data;

}