Search in sources :

Example 41 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project zxing by zxing.

the class HistoryManager method clearHistory.

void clearHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    try {
        db = helper.getWritableDatabase();
        db.delete(DBHelper.TABLE_NAME, null, null);
    } catch (SQLException sqle) {
        Log.w(TAG, sqle);
    } finally {
        close(null, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException)

Example 42 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project zxing by zxing.

the class HistoryManager method buildHistory.

/**
   * <p>Builds a text representation of the scanning history. Each scan is encoded on one
   * line, terminated by a line break (\r\n). The values in each line are comma-separated,
   * and double-quoted. Double-quotes within values are escaped with a sequence of two
   * double-quotes. The fields output are:</p>
   *
   * <ol>
   *  <li>Raw text</li>
   *  <li>Display text</li>
   *  <li>Format (e.g. QR_CODE)</li>
   *  <li>Unix timestamp (milliseconds since the epoch)</li>
   *  <li>Formatted version of timestamp</li>
   *  <li>Supplemental info (e.g. price info for a product barcode)</li>
   * </ol>
   */
CharSequence buildHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getWritableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
        DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        StringBuilder historyText = new StringBuilder(1000);
        while (cursor.moveToNext()) {
            historyText.append('"').append(massageHistoryField(cursor.getString(0))).append("\",");
            historyText.append('"').append(massageHistoryField(cursor.getString(1))).append("\",");
            historyText.append('"').append(massageHistoryField(cursor.getString(2))).append("\",");
            historyText.append('"').append(massageHistoryField(cursor.getString(3))).append("\",");
            // Add timestamp again, formatted
            long timestamp = cursor.getLong(3);
            historyText.append('"').append(massageHistoryField(format.format(timestamp))).append("\",");
            // Above we're preserving the old ordering of columns which had formatted data in position 5
            historyText.append('"').append(massageHistoryField(cursor.getString(4))).append("\"\r\n");
        }
        return historyText;
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DateFormat(java.text.DateFormat) Cursor(android.database.Cursor)

Example 43 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project zxing by zxing.

the class HistoryManager method buildHistoryItem.

public HistoryItem buildHistoryItem(int number) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
        cursor.move(number + 1);
        String text = cursor.getString(0);
        String display = cursor.getString(1);
        String format = cursor.getString(2);
        long timestamp = cursor.getLong(3);
        String details = cursor.getString(4);
        Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
        return new HistoryItem(result, display, details);
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) Result(com.google.zxing.Result)

Example 44 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project mobile-center-sdk-android by Microsoft.

the class DatabaseManagerTest method getDatabaseFailedOnce.

@Test
public void getDatabaseFailedOnce() {
    /* Mocking instances. */
    Context contextMock = mock(Context.class);
    SQLiteOpenHelper helperMock = mock(SQLiteOpenHelper.class);
    when(helperMock.getWritableDatabase()).thenThrow(new RuntimeException()).thenReturn(mock(SQLiteDatabase.class));
    /* Instantiate real instance for DatabaseManager. */
    DatabaseManager databaseManager = new DatabaseManager(contextMock, "database", "table", 1, null, null);
    databaseManager.setSQLiteOpenHelper(helperMock);
    /* Get database. */
    SQLiteDatabase database = databaseManager.getDatabase();
    /* Verify. */
    assertNotNull(database);
    verify(contextMock).deleteDatabase("database");
}
Also used : Context(android.content.Context) SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 45 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project weex-example by KalicyZhou.

the class HistoryManager method trimHistory.

public void trimHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    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);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException)

Aggregations

SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)49 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)38 Cursor (android.database.Cursor)21 Test (org.junit.Test)16 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)9 ContentValues (android.content.ContentValues)8 Result (com.google.zxing.Result)6 Context (android.content.Context)5 SQLException (android.database.SQLException)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 SharedPreferences (android.content.SharedPreferences)3 DateFormat (java.text.DateFormat)3 ArrayList (java.util.ArrayList)3 SQLiteException (android.database.sqlite.SQLiteException)2 Changes (com.pushtorefresh.storio.sqlite.Changes)2 RawQuery (com.pushtorefresh.storio.sqlite.queries.RawQuery)2 Date (java.util.Date)2 Stream (com.annimon.stream.Stream)1 TypeMappingFinder (com.pushtorefresh.storio.TypeMappingFinder)1 InsertQuery (com.pushtorefresh.storio.sqlite.queries.InsertQuery)1