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.maxwen.wallpaper.board.databases.Database.java

public List<Object> getCategoriesUnified() {
    List<Object> categories = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_CATEGORIES, null, null, null, null, null, KEY_NAME);
    if (cursor.moveToFirst()) {
        do {//from w w  w  .j a v a  2s.c om
            Category category = new Category(cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                    cursor.getInt(3) == 1);
            int count = getWallpapersCountOfCatgegory(cursor.getString(1));
            category.setNumWallpapers(count);
            categories.add(category);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return categories;
}

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

public void addBleEntry(BluetoothLowEnergyEntry ble_entry) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_SESSION_ID, ble_entry.getSessionId().toString());
    values.put(KEY_LOCATION_ID, ble_entry.getLocationId().toString());
    values.put(KEY_TIMESTAMP, ble_entry.getTimestamp());
    values.put(KEY_MAC, ble_entry.getMac());
    values.put(KEY_RSSI, ble_entry.getRssi());
    values.put(KEY_DEVICE_NAME, ble_entry.getDeviceName());
    values.put(KEY_BLE_ADV_DATA, ble_entry.getBleAdvData());

    values.put(KEY_UPLOAD_STATUS, ble_entry.getUploadStatus());

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

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

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

public void addWallpapers(@NonNull WallpaperJson wallpaper) {
    String query = "INSERT INTO " + TABLE_WALLPAPERS + " (" + KEY_NAME + "," + KEY_AUTHOR + "," + KEY_URL + ","
            + KEY_THUMB_URL + "," + KEY_CATEGORY + "," + KEY_ADDED_ON + ") VALUES (?,?,?,?,?,?);";
    SQLiteDatabase db = this.getWritableDatabase();
    SQLiteStatement statement = db.compileStatement(query);
    db.beginTransaction();//from  w  w  w  .jav a 2 s . c  om

    for (int i = 0; i < wallpaper.getWallpapers.size(); i++) {
        statement.clearBindings();
        statement.bindString(1, wallpaper.getWallpapers.get(i).name);
        statement.bindString(2, wallpaper.getWallpapers.get(i).author);
        statement.bindString(3, wallpaper.getWallpapers.get(i).url);
        statement.bindString(4,
                wallpaper.getWallpapers.get(i).thumbUrl == null ? wallpaper.getWallpapers.get(i).url
                        : wallpaper.getWallpapers.get(i).thumbUrl);
        statement.bindString(5, wallpaper.getWallpapers.get(i).category);
        statement.bindString(6, TimeHelper.getLongDateTime());
        statement.execute();
    }
    db.setTransactionSuccessful();
    db.endTransaction();
    db.close();
}

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

@Nullable
public Wallpaper getRandomWallpaper() {
    Wallpaper wallpaper = null;/*from  w ww  .j  a v  a 2s.c o m*/
    List<String> selected = getSelectedCategories();
    List<String> selection = new ArrayList<>();
    if (selected.size() == 0)
        return null;

    StringBuilder CONDITION = new StringBuilder();
    for (String item : selected) {
        if (CONDITION.length() > 0) {
            CONDITION.append(" OR ").append("LOWER(").append(KEY_CATEGORY).append(")").append(" LIKE ?");
        } else {
            CONDITION.append("LOWER(").append(KEY_CATEGORY).append(")").append(" LIKE ?");
        }
        selection.add("%" + item.toLowerCase(Locale.getDefault()) + "%");
    }

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, CONDITION.toString(),
            selection.toArray(new String[selection.size()]), null, null, "RANDOM()", "1");
    if (cursor.moveToFirst()) {
        do {
            wallpaper = new Wallpaper(cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                    cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getInt(6) == 1,
                    cursor.getLong(7));
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return wallpaper;
}

From source file:net.potterpcs.recipebook.RecipeData.java

public void updateTags(long rid, ContentValues values) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {//from w w  w.j a v a2s  . c om
            db.updateWithOnConflict(TAGS_TABLE, values, TT_RECIPE_ID + " = ?",
                    new String[] { Long.toString(rid) }, SQLiteDatabase.CONFLICT_IGNORE);
        } finally {
            db.close();
        }
    }
}

From source file:com.fan3cn.fishrecorder.ContentFragment.java

/**
 * ??//  ww w .j  a  va 2 s  .  c o  m
 * @param view
 */
private void handleCompanyEvent(final View view) {
    Button addButton = (Button) view.findViewById(R.id.button_add);
    addButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            EditText nameET = (EditText) view.findViewById(R.id.editText_com_name);
            EditText phoneET = (EditText) view.findViewById(R.id.editText_com_phone);
            EditText faxET = (EditText) view.findViewById(R.id.editText_com_fax);
            EditText emailET = (EditText) view.findViewById(R.id.editText_com_email);
            EditText addressET = (EditText) view.findViewById(R.id.editText_com_addr);
            CheckBox ckBox = (CheckBox) view.findViewById(R.id.checkBox_is_default);

            int isDefault = ckBox.isChecked() ? 1 : 0;

            String name = nameET.getText().toString();
            String phone = phoneET.getText().toString();
            String fax = faxET.getText().toString();
            String email = emailET.getText().toString();
            String address = addressET.getText().toString();

            if (name.isEmpty() || phone.isEmpty() || fax.isEmpty() || email.isEmpty() || address.isEmpty()) {
                new AlertDialog.Builder(getActivity()).setTitle("??").setPositiveButton("", null)
                        .setMessage("?").show();
                return;
            }
            SQLiteDatabase db = MainActivity.getDbHelper().getWritableDatabase();

            Cursor cursor = db.query(Constants.table.get(menuId), null, "is_default=?", new String[] { 1 + "" },
                    null, null, null);

            if (isDefault == 1 && cursor.getCount() > 0) {
                //?
                ContentValues cv1 = new ContentValues();
                cv1.put("is_default", 0);
                db.update(Constants.table.get(menuId), cv1, "is_default=?", new String[] { 1 + "" });
            }

            ContentValues cv = new ContentValues();
            cv.put("name", name);
            cv.put("tel", phone);
            cv.put("fax", fax);
            cv.put("email", email);
            cv.put("address", address);
            cv.put("is_default", isDefault);
            db.insert(Constants.table.get(menuId), null, cv);
            //??  
            db.close();

            Toast.makeText(getActivity(), "?!", Toast.LENGTH_SHORT).show();
        }
    });

    Button clearButton = (Button) view.findViewById(R.id.button_clear);

    clearButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            EditText nameET = (EditText) view.findViewById(R.id.editText_com_name);
            EditText phoneET = (EditText) view.findViewById(R.id.editText_com_phone);
            EditText faxET = (EditText) view.findViewById(R.id.editText_com_fax);
            EditText emailET = (EditText) view.findViewById(R.id.editText_com_email);
            EditText addressET = (EditText) view.findViewById(R.id.editText_com_addr);
            CheckBox ckBox = (CheckBox) view.findViewById(R.id.checkBox_is_default);

            nameET.setText("");
            phoneET.setText("");
            faxET.setText("");
            emailET.setText("");
            addressET.setText("");
            ckBox.setChecked(false);
        }
    });

}

From source file:net.potterpcs.recipebook.RecipeData.java

public void updateDirections(long rid, ContentValues values) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {//from   w w  w.  ja  v a  2  s  .c  o m
            db.updateWithOnConflict(DIRECTIONS_TABLE, values, DT_RECIPE_ID + " = ?",
                    new String[] { Long.toString(rid) }, SQLiteDatabase.CONFLICT_IGNORE);
        } finally {
            db.close();
        }
    }
}

From source file:net.potterpcs.recipebook.RecipeData.java

public void updateIngredients(long rid, ContentValues values) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {//from   www . j a v a2 s . c  o m
            db.updateWithOnConflict(INGREDIENTS_TABLE, values, IT_RECIPE_ID + " = ?",
                    new String[] { Long.toString(rid) }, SQLiteDatabase.CONFLICT_IGNORE);
        } finally {
            db.close();
        }
    }
}

From source file:ru.gkpromtech.exhibition.db.Table.java

public List<T> select(String selection, String[] selectionArgs, String groupBy, String having, String orderBy,
        String limit) {//  w ww.  j  a  v a  2 s.co m
    List<T> result = new ArrayList<>();

    SQLiteDatabase db = mSqlHelper.getReadableDatabase();
    Cursor cursor = db.query(mTableName, mColumns, selection, selectionArgs, groupBy, having, orderBy, limit);
    //noinspection TryFinallyCanBeTryWithResources
    try {
        while (cursor.moveToNext()) {
            T entity = mEntityClass.newInstance();
            for (int i = 0; i < mFields.length; ++i)
                fillFieldValue(mType[i], mFields[i], entity, cursor, i);
            result.add(entity);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cursor.close();
        db.close();
    }

    return result;
}