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:net.smart_json_database.JSONDatabase.java

/**
 * Deletes all content without the meta data
 * /*from w ww.ja v  a2 s.c  o m*/
 * @return true if all delte operations are succesfull
 */
public boolean clearAllTables() {
    boolean returnValue = true;
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    try {
        db.beginTransaction();
        //db.execSQL("DELETE FROM " + TABLE_Meta);
        db.execSQL("DELETE FROM " + TABLE_TAG);
        db.execSQL("DELETE FROM " + TABLE_JSON_DATA);
        db.execSQL("DELETE FROM " + TABLE_REL_TAG_JSON_DATA);
        db.setTransactionSuccessful();
    } catch (Exception e) {
        returnValue = false;
    } finally {
        db.endTransaction();
        db.close();
    }
    return returnValue;
}

From source file:com.teinvdlugt.android.greekgods.AllPeopleActivity.java

private void refresh() {
    new AsyncTask<Void, Void, List<Person>>() {
        @Override// www . j ava  2s . c  om
        protected List<Person> doInBackground(Void... params) {
            if (searchQuery == null)
                searchQuery = "";

            List<Person> result = new ArrayList<>();

            SQLiteDatabase db = null;
            Cursor c = null;
            try {
                db = openOrCreateDatabase("data", 0, null);
                String[] columns = { "personId", "name" };
                String selection = "name LIKE '" + searchQuery + "%'";
                c = db.query("people", columns, selection, null, null, null, "name");
                int idColumn = c.getColumnIndex("personId");
                int nameColumn = c.getColumnIndex("name");

                c.moveToFirst();
                do {
                    Person p = new Person();
                    p.setId(c.getInt(idColumn));
                    p.setName(c.getString(nameColumn));
                    result.add(p);
                } while (c.moveToNext());
            } catch (SQLiteException e) {
                return null;
            } catch (CursorIndexOutOfBoundsException ignored) {
            } finally {
                if (c != null)
                    c.close();
                if (db != null)
                    db.close();
            }

            return result;
        }

        @Override
        protected void onPostExecute(List<Person> persons) {
            adapter.setData(persons);
        }
    }.execute();
}

From source file:org.thomnichols.android.gmarks.WebViewCookiesDB.java

List<Cookie> getCookies() {
    List<Cookie> cookies = new ArrayList<Cookie>();
    SQLiteDatabase db = openDatabase(SQLiteDatabase.OPEN_READONLY);
    if (db == null)
        return cookies;

    try {//from www . j a  v  a 2s .  c  om
        db.execSQL("PRAGMA read_uncommitted = true;");
        Cursor cursor = db.query(TABLE_NAME, COLUMNS, null, null, null, null, null);

        while (cursor.moveToNext()) {
            BasicClientCookie c = new BasicClientCookie(cursor.getString(COL_NAME),
                    cursor.getString(COL_VALUE));
            c.setDomain(cursor.getString(COL_DOMAIN));
            c.setPath(cursor.getString(COL_PATH));
            Long expiry = cursor.getLong(COL_EXPIRES);
            if (expiry != null)
                c.setExpiryDate(new Date(expiry));
            c.setSecure(cursor.getShort(COL_SECURE) == 1);
            Log.d(TAG, "Got cookie: " + c.getName());
            cookies.add(c);
        }
        cursor.close();

        //         cursor = db.query(TABLE_NAME, new String[] {"count(name)"}, null, null, null, null, null);
        //         cursor.moveToFirst();
        //         Log.d("WEBVIEW DB QUERY", "COunt: " + cursor.getLong(0) );
        //          cursor.close();
        return cookies;
    } finally {
        db.close();
    }
}

From source file:com.maxwen.wallpaper.board.databases.Database.java

public void addWallpapers(@NonNull WallpaperJson wallpapers) {
    SQLiteDatabase db = this.getWritableDatabase();
    long insertTime = System.currentTimeMillis();
    for (int i = 0; i < wallpapers.getWallpapers.size(); i++) {
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, wallpapers.getWallpapers.get(i).name);
        values.put(KEY_AUTHOR, wallpapers.getWallpapers.get(i).author);
        values.put(KEY_URL, wallpapers.getWallpapers.get(i).url);
        values.put(KEY_THUMB_URL, wallpapers.getWallpapers.get(i).thumbUrl);
        values.put(KEY_CATEGORY, wallpapers.getWallpapers.get(i).category);
        values.put(KEY_ADDED_ON, insertTime);

        db.insert(TABLE_WALLPAPERS, null, values);
    }// w  ww.ja  v  a  2  s .  c  om
    db.close();
}

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

public void addLocation(GPSLocation location) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_LOCATION_ID, location.getLocationId().toString());
    values.put(KEY_SESSION_ID, location.getSessionId().toString());
    values.put(KEY_TIMESTAMP, location.getTimestamp());
    values.put(KEY_LATITUDE, location.getLatitude());
    values.put(KEY_LONGITUDE, location.getLongitude());
    values.put(KEY_SPEED, location.getSpeed());
    values.put(KEY_BEARING, location.getBearing());
    values.put(KEY_ALTITUDE, location.getAltitude());
    values.put(KEY_ACCURACY, location.getAccuracy());
    values.put(KEY_UPLOAD_STATUS, location.getUploadStatus());

    // Inserting Row
    db.insertOrThrow(TABLE_LOCATIONS, null, values);

    db.close(); // Closing database connection
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static void expireReadMessages(Context context, boolean expireAll, long expireTime) {

    DBHelper db = new DBHelper(context);
    SQLiteDatabase dbwrite = db.getWritableDatabase();

    // Get all the expired messages so we can delete bodies and attachments
    long currentTime = System.currentTimeMillis();
    String q = null;/* w  w  w.  j  av  a 2 s. co  m*/

    if (expireAll) {
        q = "SELECT _id, subscribed_group_id, has_attachments, attachments_fnames " + "FROM headers "
                + "WHERE read=1 AND catched=1";
    } else {
        q = "SELECT _id, subscribed_group_id, has_attachments, attachments_fnames " + "FROM headers "
                + "WHERE read=1 AND catched=1 AND read_unixdate < " + currentTime + " - " + expireTime;
    }

    Cursor c = dbwrite.rawQuery(q, null);

    int count = c.getCount();
    c.moveToFirst();
    String groupname;

    for (int i = 0; i < count; i++) {

        groupname = getGroupNameFromId(c.getInt(1) /*subscribed_group_id*/, context);
        FSUtils.deleteCacheMessage(c.getInt(0)/* _id */, groupname);

        if (c.getInt(2)/*has_attach*/ == 1) {
            FSUtils.deleteAttachments(c.getString(3) /*attachments_fnames*/, groupname);
        }

        c.moveToNext();
    }

    if (expireAll)
        q = "DELETE FROM headers WHERE read=1";
    else
        q = "DELETE FROM headers WHERE read=1 AND read_unixdate < " + currentTime + " - " + expireTime;
    dbwrite.execSQL(q);
    c.close();
    dbwrite.close();
    db.close();
}

From source file:net.smart_json_database.JSONDatabase.java

private void updateTagMap() {
    String sql = "SELECT * FROM " + TABLE_TAG;
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor c = db.rawQuery(sql, new String[] {});
    if (c.getCount() > 0) {
        int col_id = c.getColumnIndex("tag_uid");
        int col_name = c.getColumnIndex("name");

        c.moveToFirst();/*from   w  w w .j a  v a  2s .  c o  m*/
        do {
            tags.put(c.getString(col_name), new Integer(c.getInt(col_id)));
            invertedTags.put(new Integer(c.getInt(col_id)), c.getString(col_name));
        } while (c.moveToNext());
    }
    c.close();
    db.close();
}

From source file:net.olejon.mdapp.MedicationActivity.java

private boolean isFavorite() {
    SQLiteDatabase sqLiteDatabase = new MedicationsFavoritesSQLiteHelper(mContext).getReadableDatabase();

    String[] queryColumns = { MedicationsFavoritesSQLiteHelper.COLUMN_NAME,
            MedicationsFavoritesSQLiteHelper.COLUMN_MANUFACTURER };
    Cursor cursor = sqLiteDatabase.query(MedicationsFavoritesSQLiteHelper.TABLE, queryColumns,
            MedicationsFavoritesSQLiteHelper.COLUMN_NAME + " = " + mTools.sqe(medicationName) + " AND "
                    + MedicationsFavoritesSQLiteHelper.COLUMN_MANUFACTURER + " = "
                    + mTools.sqe(medicationManufacturer),
            null, null, null, null);/*from  w  ww  .jav a  2  s .co m*/

    int count = cursor.getCount();

    cursor.close();
    sqLiteDatabase.close();

    return (count != 0);
}

From source file:com.barcamppenang2014.tabfragment.ProfileFragment.java

License:asdf

public String[] fillTextField() {
    String[] myInfo = new String[5];

    MyDatabase database = new MyDatabase(getActivity());
    SQLiteDatabase sqliteDatabase = database.getReadableDatabase();
    String sql = "SELECT * FROM USERPROFILE;";
    Cursor retrieved = sqliteDatabase.rawQuery(sql, null);

    // Log.d("yc","row of cursor in database is "+
    // Integer.toString(retrieved.getCount()));

    // If cursor is not null
    while (retrieved.moveToNext()) {

        myInfo[0] = retrieved.getString(retrieved.getColumnIndex("MYNAME"));
        myInfo[1] = retrieved.getString(retrieved.getColumnIndex("MYEMAIL"));
        myInfo[2] = retrieved.getString(retrieved.getColumnIndex("MYPHONE"));
        myInfo[3] = retrieved.getString(retrieved.getColumnIndex("MYPROFESSION"));
        myInfo[4] = retrieved.getString(retrieved.getColumnIndex("MYFBID"));
        // myInfo[5] =
        // retrieved.getString(retrieved.getColumnIndex("MYURI"));

    }/*from w  w  w  . java 2s  .  c o  m*/

    retrieved.close();
    database.close();
    sqliteDatabase.close();

    return myInfo;

}

From source file:com.yammy.meter.MainActivity.java

private void getVehVal() {
    /**/*from w ww  .  j  a  v a  2  s .c o  m*/
      * get value from saved preferences
      */
    try {
        prefs = getSharedPreferences(prefName, MODE_PRIVATE);
        nopol.setText(prefs.getString("nopol", null));
        idkendaraan = Integer.parseInt(prefs.getString("idkendaraan", null));
        start.setEnabled(true);
    } catch (Exception e) {
        idkendaraan = prefs.getInt("idkendaraan", 0);
        start.setEnabled(false);
    }

    dbHelper = new MySQLHelper(getApplicationContext());
    SQLiteDatabase jdb = dbHelper.getReadableDatabase();
    try {
        Cursor cursor = jdb.rawQuery("SELECT SUM(jarak) FROM perjalanan WHERE id_kendaraan=" + idkendaraan,
                null);
        if (cursor.moveToFirst()) {
            try {
                totalJarakDitempuhKendaraan = Double.parseDouble(String.format("%.2f", cursor.getString(0)));
            } catch (NumberFormatException e) {
                totalJarakDitempuhKendaraan = Double.parseDouble(cursor.getString(0));
            } catch (Exception e) {
                totalJarakDitempuhKendaraan = cursor.getDouble(0);
            }
        } else {
            totalJarakDitempuhKendaraan = 0.0;
        }
        Toast.makeText(getBaseContext(), "Sudah ditempuh : " + totalJarakDitempuhKendaraan + " m",
                Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), "Belum pernah melakukan perjalanan", Toast.LENGTH_SHORT).show();
    }
    jdb.close();
}