Example usage for android.database.sqlite SQLiteOpenHelper getReadableDatabase

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

Introduction

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

Prototype

public SQLiteDatabase getReadableDatabase() 

Source Link

Document

Create and/or open a database.

Usage

From source file:net.eledge.android.toolkit.db.abstracts.Dao.java

public Dao(Class<E> clazz, SQLiteOpenHelper helper) {
    this.clazz = clazz;
    db = helper.getReadableDatabase();
}

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

/**
 * Get readable database//  ww w. j  av a  2  s .c o m
 * 
 * @param helper
 * @return readable database or null if it failed to create/open
 */
protected SQLiteDatabase getReadable(SQLiteOpenHelper helper) {
    try {
        return helper.getReadableDatabase();
    } catch (SQLiteException e1) {
        // Make second attempt
        try {
            return helper.getReadableDatabase();
        } catch (SQLiteException e2) {
            return null;
        }
    }
}

From source file:com.android.unit_tests.DbSSLSessionCacheTest.java

/** 
 * We want to test the actual database write - the actual hooking into 
 * low-level SSL is tested. /* ww  w .ja  va  2 s  .co m*/
 */
@LargeTest
public void testSslCacheAdd() throws Exception {
    // Let's verify the database has the rows.
    // Use internal details of the implementation - could make the field
    // visible for testing, but it's same.

    // Use default database
    DbSSLSessionCache cache = DbSSLSessionCache.getInstanceForPackage(getContext());
    cache.clear();

    makeRequestInNewContext("https://www.google.com");

    // Verify the key was inserted
    SQLiteOpenHelper helper = new DatabaseHelper(getContext());
    Cursor query = null;
    try {
        query = helper.getReadableDatabase().query(DbSSLSessionCache.SSL_CACHE_TABLE,
                new String[] { "hostport" }, null, null, null, null, null);

        assertTrue(query.moveToFirst()); // one row inserted
        String hostPort = query.getString(0);
        assertEquals(hostPort, "www.google.com:443");
    } finally {
        query.close();
    }
}

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

public boolean hasHistoryItems() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;/*w  ww.  ja  va 2  s .  c  om*/
    Cursor cursor = null;
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COUNT_COLUMN, null, null, null, null, null);
        cursor.moveToFirst();
        return cursor.getInt(0) > 0;
    } finally {
        close(cursor, db);
    }
}

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

public HistoryItem buildHistoryItem(int number) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;/* w  ww. ja  v a 2  s. com*/
    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);
        return new HistoryItem(result, display);
    } finally {
        close(cursor, db);
    }
}

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

public List<HistoryItem> buildHistoryItems() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    List<HistoryItem> items = new ArrayList<HistoryItem>();
    SQLiteDatabase db = null;//from  w ww. j ava2 s .  c om
    Cursor cursor = null;
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null,
                DBHelper.TIMESTAMP_COL + " DESC");
        while (cursor.moveToNext()) {
            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);
            // items.add(new HistoryItem(result, display, details));
            items.add(new HistoryItem(result, display));
        }
    } finally {
        close(cursor, db);
    }
    return items;
}

From source file:com.concentricsky.android.khanacademy.app.ManageDownloadsActivity.java

private Cursor getDisplayOptionsCursor(SQLiteOpenHelper helper) {
    SQLiteDatabase db = helper.getReadableDatabase();

    String sql = "select distinct topic._id as _id, topic.title as title from topic, topicvideo, video where video.download_status>? and topicvideo.video_id=video.readable_id and topicvideo.topic_id=topic._id group by title";
    String[] selectionArgs = { String.valueOf(Video.DL_STATUS_NOT_STARTED) };
    Cursor mainCursor = db.rawQuery(sql, selectionArgs);

    sql = "select '-1' as _id, 'All Videos' as title";
    Cursor headerCursor = db.rawQuery(sql, null);

    MergeCursor cursor = new MergeCursor(new Cursor[] { headerCursor, mainCursor });
    return cursor;
}

From source file:com.concentricsky.android.khanacademy.util.OfflineVideoManager.java

public int getDownloadCountForTopic(SQLiteOpenHelper dbh, String topicId, int depth) {
    Log.d(LOG_TAG, "getDownloadCountForTopic");

    int result = 0;
    SQLiteDatabase db = dbh.getReadableDatabase();

    for (int i = 0; i < depth; ++i) {
        String sql = buildDownloadCountQuery(i);
        Cursor c = db.rawQuery(sql, new String[] { topicId });
        c.moveToFirst();//from   w  ww.j  a va2s  . c  om
        result += c.getInt(0);
        Log.d(LOG_TAG, " result is " + result);
        c.close();
    }

    return result;
}

From source file:com.concentricsky.android.khanacademy.app.ManageDownloadsActivity.java

private Cursor getCursor(SQLiteOpenHelper helper, User currentUser, String topicTitle) {
    String userId = currentUser == null ? "" : currentUser.getNickname();

    String sql = "select distinct(video._id) as _id, video.youtube_id, video.readable_id, video.title, video.dlm_id "
            + ", uservideo.seconds_watched, uservideo.completed " +
            // If we join on topicvideo without filtering to a single topic id, we get a giant n^2 query that takes forever.
            "from video" + (topicTitle != null ? ", topicvideo, topic " : " ")
            + "left outer join uservideo on uservideo.video_id = video.readable_id and uservideo.user_id=? "
            + "where video.download_status>? ";

    String[] selectionArgs;/*w w w. j a va2s .  co m*/
    if (topicTitle != null) {
        sql += " and topicvideo.topic_id=topic._id and topic.title=? and topicvideo.video_id=video.readable_id ";
        selectionArgs = new String[] { userId, String.valueOf(Video.DL_STATUS_NOT_STARTED), topicTitle };
    } else {
        selectionArgs = new String[] { userId, String.valueOf(Video.DL_STATUS_NOT_STARTED) };
    }
    sql += "order by video.parentTopic_id, video.seq";

    return helper.getReadableDatabase().rawQuery(sql, selectionArgs);
}