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 deleteUsers_specialization(JSONArray data) {
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;//from   ww  w .ja v  a2  s . c  o m
    try {
        for (i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);
            db.delete(DBSchema.TABLE_USERS_SPECIALIZATION,
                    DBSchema.USERS_SPECIALIZATION_SPECIALIZATION_ID + " =? AND "
                            + DBSchema.USERS_SPECIALIZATION_USER_ID + " =? ",
                    new String[] {
                            String.valueOf(item.getLong(DBSchema.USERS_SPECIALIZATION_SPECIALIZATION_ID)),
                            String.valueOf(item.getLong(DBSchema.USERS_SPECIALIZATION_USER_ID)) });
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.close();
    return i;
}

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

public List<Location> getAllLocations() {
    String username = context/*from www  .  java2s  .com*/
            .getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE)
            .getString(context.getString(R.string.key_saved_username), null);
    if (isAgent(username)) {
        return getAllLocationsOf(getUserId(username));
    } else {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(DBSchema.TABLE_LOCATION,
                new String[] { DBSchema.LOCATION_ID, DBSchema.LOCATION_ADDRESS_ID, DBSchema.LOCATION_NAME },
                DBSchema.STATUS + " !=? ", new String[] { String.valueOf(-1) }, null, null,
                DBSchema.LOCATION_NAME + " COLLATE NOCASE", null);
        ArrayList<Location> location;
        location = new ArrayList<>();
        if ((cursor != null) && (cursor.getCount() > 0)) {

            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                location.add(new Location(cursor.getLong(0), cursor.getLong(1), cursor.getString(2), this));
            }

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

        }
        return location;
    }

}

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

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

            ContentValues values = new ContentValues();
            if (!item.isNull(DBSchema.CATEGORY_ID))
                values.put(DBSchema.CATEGORY_ID, item.getLong(DBSchema.CATEGORY_ID));
            if (!item.isNull(DBSchema.CATEGORY_NAME))
                values.put(DBSchema.CATEGORY_NAME, item.getString(DBSchema.CATEGORY_NAME));
            values.put(DBSchema.MODIFIED, DBSchema.MODIFIED_NO);

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

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

public List<Location> getAllLocationsOf(long agentID) {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_LOCATION,
            new String[] { DBSchema.LOCATION_ID, DBSchema.LOCATION_ADDRESS_ID, DBSchema.LOCATION_NAME },
            DBSchema.STATUS + " !=? AND " + DBSchema.LOCATION_AGENT_ID + " =? ",
            new String[] { String.valueOf(-1), String.valueOf(agentID) }, null, null,
            DBSchema.LOCATION_NAME + " COLLATE NOCASE", null);
    ArrayList<Location> location;
    location = new ArrayList<>();
    if ((cursor != null) && (cursor.getCount() > 0)) {

        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            location.add(new Location(cursor.getLong(0), cursor.getLong(1), cursor.getString(2), this));
        }//  ww w. j  ava  2 s  .c  om

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

    }
    return location;

}

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

private long setSpecialization(JSONArray data) {
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;//  w w  w  .ja v  a  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.SPECIALIZATION_ID))
                values.put(DBSchema.SPECIALIZATION_ID, item.getLong(DBSchema.SPECIALIZATION_ID));
            if (!item.isNull(DBSchema.SPECIALIZATION_NAME))
                values.put(DBSchema.SPECIALIZATION_NAME, item.getString(DBSchema.SPECIALIZATION_NAME));
            values.put(DBSchema.MODIFIED, DBSchema.MODIFIED_NO);
            db.insertWithOnConflict(DBSchema.TABLE_SPECIALIZATION, null, values, 5);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.close();
    return i;

}

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

private long setSyncLocation_category(JSONArray data) {
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;/*  w w w .  j  ava2s.c om*/
    try {
        for (i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);
            ContentValues values = new ContentValues();
            values.put(DBSchema.MODIFIED, DBSchema.MODIFIED_NO);
            db.update(DBSchema.TABLE_LOCATION_CATEGORY, values,
                    DBSchema.LOCATION_CATEGORY_LOCATION_ID + " =? AND " + DBSchema.LOCATION_CATEGORY_CATEGORY_ID
                            + " =? ",
                    new String[] { String.valueOf(item.getLong(DBSchema.LOCATION_CATEGORY_LOCATION_ID)),
                            String.valueOf(item.getLong(DBSchema.LOCATION_CATEGORY_CATEGORY_ID)) });
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.close();
    return i;
}

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

private long setSyncUsers_specialization(JSONArray data) {
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;//from w ww .  j a  va 2  s. co m
    try {
        for (i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);
            ContentValues values = new ContentValues();
            values.put(DBSchema.MODIFIED, DBSchema.MODIFIED_NO);
            db.update(DBSchema.TABLE_USERS_SPECIALIZATION, values,
                    DBSchema.USERS_SPECIALIZATION_USER_ID + " =? AND "
                            + DBSchema.USERS_SPECIALIZATION_SPECIALIZATION_ID + " =? ",
                    new String[] { String.valueOf(item.getLong(DBSchema.USERS_SPECIALIZATION_USER_ID)),
                            String.valueOf(item.getLong(DBSchema.USERS_SPECIALIZATION_SPECIALIZATION_ID)) });
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.close();
    return -1;
}

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

private long setUsers_specialization(JSONArray data) {
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;/*www. j  a  v  a  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.USERS_SPECIALIZATION_USER_ID))
                values.put(DBSchema.USERS_SPECIALIZATION_USER_ID,
                        item.getLong(DBSchema.USERS_SPECIALIZATION_USER_ID));
            if (!item.isNull(DBSchema.USERS_SPECIALIZATION_SPECIALIZATION_ID))
                values.put(DBSchema.USERS_SPECIALIZATION_SPECIALIZATION_ID,
                        item.getString(DBSchema.USERS_SPECIALIZATION_SPECIALIZATION_ID));
            db.insertWithOnConflict(DBSchema.TABLE_USERS_SPECIALIZATION, null, values, 5);
            values.put(DBSchema.MODIFIED, DBSchema.MODIFIED_NO);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.close();
    return -1;
}

From source file:org.lastmilehealth.collect.android.tasks.BluetoothService.java

private void instancesProcessing() {
    //fill db with new values
    String dbpath = Collect.TMP_PATH + "/instances.db";
    SQLiteDatabase db = SQLiteDatabase.openDatabase(dbpath, null, SQLiteDatabase.OPEN_READONLY);
    Cursor cursor = db.query(InstanceProvider.INSTANCES_TABLE_NAME, null, null, null, null, null, null);
    Log.d("~", "cursor.getCount(): " + cursor.getCount());

    cursor.moveToPosition(-1);/*from w  w  w  . ja  v  a 2 s .c o  m*/
    while (cursor.moveToNext()) {
        String newInstanceName = cursor
                .getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.DISPLAY_NAME));
        String instanceFilePath = cursor
                .getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.INSTANCE_FILE_PATH));
        String newFilePath;

        if (new File(instanceFilePath).exists()) { //instance with this path already exist, rare case but not impossible
            newFilePath = getInstanceFilePath(instanceFilePath, 1);
            Log.d(TAG, "instance already exists, new path: " + newFilePath);

            String num = newFilePath.substring(newFilePath.lastIndexOf("(") + 1, newFilePath.lastIndexOf(")"));
            newInstanceName += "(" + num + ")";
            //Log.d(TAG, "newInstanceName: "+newInstanceName);

            final String fromName = instanceFilePath.substring(instanceFilePath.lastIndexOf("instances/") + 10);
            final String toName = newFilePath.substring(instanceFilePath.lastIndexOf("instances/") + 10);

            //raname file in tmp folder to prepare for copy direcory
            try {
                Log.d(TAG, "rename " + fromName + " to " + toName);
                org.apache.commons.io.FileUtils.copyFile(new File(Collect.TMP_PATH, fromName),
                        new File(Collect.TMP_PATH, toName));
                org.apache.commons.io.FileUtils.deleteQuietly(new File(Collect.TMP_PATH, fromName));
            } catch (Exception e) {
            }
        } else {
            newFilePath = new File(Collect.INSTANCES_PATH,
                    instanceFilePath.substring(instanceFilePath.lastIndexOf("/"))).getAbsolutePath();
            Log.d(TAG, "not exist, new path " + newFilePath);
        }

        String submissionUri = null;
        if (!cursor.isNull(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.SUBMISSION_URI))) {
            submissionUri = cursor
                    .getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.SUBMISSION_URI));
        }

        //add to db with new name, it it was duplicated
        ContentValues values = new ContentValues();
        values.put(InstanceProviderAPI.InstanceColumns.DISPLAY_NAME, newInstanceName);
        values.put(InstanceProviderAPI.InstanceColumns.SUBMISSION_URI, submissionUri);
        values.put(InstanceProviderAPI.InstanceColumns.INSTANCE_FILE_PATH, newFilePath);
        values.put(InstanceProviderAPI.InstanceColumns.JR_FORM_ID,
                cursor.getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.JR_FORM_ID)));
        values.put(InstanceProviderAPI.InstanceColumns.JR_VERSION,
                cursor.getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.JR_VERSION)));
        values.put(InstanceProviderAPI.InstanceColumns.STATUS,
                cursor.getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.STATUS)));
        values.put(InstanceProviderAPI.InstanceColumns.CAN_EDIT_WHEN_COMPLETE, cursor
                .getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.CAN_EDIT_WHEN_COMPLETE)));

        Log.d(TAG, "insert new instance record: " + newInstanceName + " with path :" + newFilePath);
        Collect.getInstance().getContentResolver().insert(InstanceProviderAPI.InstanceColumns.CONTENT_URI,
                values);
    }
    cursor.close();
    db.close();

    //copy directory after deleting metadata, clear all temporary data
    org.apache.commons.io.FileUtils.deleteQuietly(new File(Collect.TMP_PATH, "instances.db"));
    org.apache.commons.io.FileUtils.deleteQuietly(new File(Collect.ZIP_PATH));
    try {
        org.apache.commons.io.FileUtils.copyDirectory(new File(Collect.TMP_PATH),
                new File(Collect.INSTANCES_PATH));
        org.apache.commons.io.FileUtils.deleteDirectory(new File(Collect.TMP_PATH));
    } catch (Exception e) {
    }
}

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

private long setItem(JSONArray data) {
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;// w w  w.j  a va  2  s .  co m
    try {
        for (i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);
            ContentValues values = new ContentValues();
            if (!item.isNull(DBSchema.ITEM_ID))
                values.put(DBSchema.ITEM_ID, item.getLong(DBSchema.ITEM_ID));
            if (!item.isNull(DBSchema.ITEM_LABEL))
                values.put(DBSchema.ITEM_LABEL, item.getString(DBSchema.ITEM_LABEL));
            if (!item.isNull(DBSchema.ITEM_FLOWCHART_ID))
                values.put(DBSchema.ITEM_FLOWCHART_ID, item.getString(DBSchema.ITEM_FLOWCHART_ID));
            if (!item.isNull(DBSchema.ITEM_TYPE))
                values.put(DBSchema.ITEM_TYPE, item.getString(DBSchema.ITEM_TYPE));
            values.put(DBSchema.MODIFIED, DBSchema.MODIFIED_NO);

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