Example usage for android.database.sqlite SQLiteOpenHelper getWritableDatabase

List of usage examples for android.database.sqlite SQLiteOpenHelper getWritableDatabase

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteOpenHelper getWritableDatabase.

Prototype

public SQLiteDatabase getWritableDatabase() 

Source Link

Document

Create and/or open a database that will be used for reading and writing.

Usage

From source file:com.hemou.android.core.sync.persistence.DatabaseCache.java

/**
 * Get writable database/*from  www .  ja v a  2s .co  m*/
 * 
 * @param helper
 * @return writable database or null if it failed to create/open
 */
protected SQLiteDatabase getWritable(SQLiteOpenHelper helper) {
    try {
        return helper.getWritableDatabase();
    } catch (SQLiteException e1) {
        // Make second attempt
        try {
            return helper.getWritableDatabase();
        } catch (SQLiteException e2) {
            return null;
        }
    }
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public JSONArray listarTodosQr() {
    JSONArray listQr = new JSONArray();
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;//  w  w w. ja v a 2 s. c o m
    db = helper.getWritableDatabase();
    Cursor c = db.rawQuery(
            "SELECT " + DBHelper.TEXT_COL + ", " + DBHelper.TIMESTAMP_COL + " FROM " + DBHelper.TABLE_NAME,
            null);
    if (c != null) {
        c.moveToFirst();
        do {
            String data = c.getString(c.getColumnIndex(DBHelper.TIMESTAMP_COL));
            String incri = c.getString(c.getColumnIndex(DBHelper.TEXT_COL));
            JSONObject inscritos = new JSONObject();
            try {
                inscritos.put("id_inscrito", incri.toString());
                inscritos.put("data_credenciado", data.toString());
                listQr.put(inscritos);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } while (c.moveToNext());
    }
    c.close();
    return listQr;
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public void deletePrevious(String text) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;/*  w w  w . j av a2s. com*/
    try {
        db = helper.getWritableDatabase();
        db.delete(DBHelper.TABLE_NAME, DBHelper.TEXT_COL + "= ?", new String[] { text });
    } finally {
        close(null, db);
    }
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public void clearHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;/*w  w  w.ja  v a 2s . c om*/
    try {
        db = helper.getWritableDatabase();
        db.delete(DBHelper.TABLE_NAME, null, null);
    } finally {
        close(null, db);
    }
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public boolean historyIsVazio() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;//from   www . j  a  v  a2  s  .  c om
    Cursor c = null;
    db = helper.getWritableDatabase();
    c = db.rawQuery("SELECT count(" + DBHelper.ID_COL + ") FROM " + DBHelper.TABLE_NAME, null);
    if (c != null && c.moveToFirst() && c.getInt(0) > 0) {
        Log.i("Script: ", "Contem dados no histrico!");
        return true;
    }
    Log.i("Script: ", "No contem nunhum dado no histrico!");
    return false;
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public boolean buscarDado(String dado) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;//  w  w w.  j a  va2 s .  c o m
    Cursor c = null;
    db = helper.getWritableDatabase();
    c = db.rawQuery("SELECT " + DBHelper.ID_INSCRITOS + " FROM " + DBHelper.INSCRITOS + " WHERE "
            + DBHelper.ID_INSCRITOS + " = '" + dado + "'", null);
    if (c != null && c.getCount() > 0) {
        c.moveToFirst();
        Log.i("Script: ", "Busca encontrada! " + dado);
        c.close();
        return true;
    } else {
        Log.i("Script: ", "Busca no encontrada! " + dado);
        c.close();
        return false;
    }
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public void addHistoryItem(Result result, ResultHandler handler, String data) {
    // Do not save this item to the history if the preference is turned off,
    // or the contents are
    // considered secure.
    if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true) || handler.areContentsSecure()) {
        return;/*from www  . j a va 2s .  c o m*/
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
    if (!prefs.getBoolean(PreferencesActivity.KEY_REMEMBER_DUPLICATES, false)) {
        deletePrevious(result.getText());
    }

    ContentValues values = new ContentValues();
    values.put(DBHelper.TEXT_COL, result.getText());
    values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
    values.put(DBHelper.DISPLAY_COL, handler.getDisplayContents().toString());
    values.put(DBHelper.TIMESTAMP_COL, data.toString());

    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    try {
        db = helper.getWritableDatabase();
        // Insert the new entry into the DB.
        db.insert(DBHelper.TABLE_NAME, null, values);
    } finally {
        close(null, db);
    }
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public JSONArray listarUmQr(String dado) {
    JSONArray listQr = new JSONArray();
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;/*from   w  w  w .j  a  va2  s .  c  o  m*/
    Cursor c = null;
    db = helper.getWritableDatabase();
    c = db.rawQuery("SELECT " + DBHelper.TEXT_COL + ", " + DBHelper.TIMESTAMP_COL + " FROM "
            + DBHelper.TABLE_NAME + " WHERE " + DBHelper.TEXT_COL + " = '" + dado + "';", null);
    if (c != null) {
        c.moveToFirst();

        // String data =
        // c.getString(c.getColumnIndex(DBHelper.TIMESTAMP_COL));
        String inscr = c.getString(c.getColumnIndex(DBHelper.TEXT_COL));

        JSONObject inscrito = new JSONObject();
        try {
            inscrito.put("id_inscrito", inscr.toString());
            inscrito.put("data_credenciado", c.getString(c.getColumnIndex(DBHelper.TIMESTAMP_COL)));
            listQr.put(inscrito);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        c.close();
    }
    return listQr;
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public void deleteHistoryItem(int number) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;//  ww  w  .j av a  2  s .  c  o m
    Cursor cursor = null;
    try {
        db = helper.getWritableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, ID_COL_PROJECTION, null, null, null, null,
                DBHelper.TIMESTAMP_COL + " DESC");
        cursor.move(number);
        db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
    } finally {
        close(cursor, db);
    }
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public void trimHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;/*from w w w  .  jav a 2s  . c  o  m*/
    Cursor cursor = null;
    try {
        db = helper.getWritableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, ID_COL_PROJECTION, null, null, null, null,
                DBHelper.TIMESTAMP_COL + " DESC");
        cursor.move(MAX_ITEMS);
        while (cursor.moveToNext()) {
            String id = cursor.getString(0);
            Log.i(TAG, "Deleting scan history ID " + id);
            db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + id, null);
        }
    } catch (SQLiteException sqle) {
        // We're seeing an error here when called in
        // CaptureActivity.onCreate() in rare cases
        // and don't understand it. First theory is that it's transient so
        // can be safely ignored.
        Log.w(TAG, sqle);
        // continue
    } finally {
        close(cursor, db);
    }
}