Example usage for android.database.sqlite SQLiteDatabase rawQuery

List of usage examples for android.database.sqlite SQLiteDatabase rawQuery

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase rawQuery.

Prototype

public Cursor rawQuery(String sql, String[] selectionArgs) 

Source Link

Document

Runs the provided SQL and returns a Cursor over the result set.

Usage

From source file:org.droidparts.util.PersistUtils.java

public static boolean dropTables(SQLiteDatabase db, String... optionalTableNames) {
    HashSet<String> tableNames = new HashSet<String>();
    if (optionalTableNames.length == 0) {
        Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
        while (c.moveToNext()) {
            tableNames.add(c.getString(0));
        }//from www. j ava2 s  .c  om
        c.close();
    } else {
        tableNames.addAll(asList(optionalTableNames));
    }
    ArrayList<String> statements = new ArrayList<String>();
    for (String tableName : tableNames) {
        statements.add("DROP TABLE IF EXISTS " + tableName + ";");
    }
    return executeStatements(db, statements);
}

From source file:org.zoumbox.mh_dla_notifier.sp.PublicScriptsProxy.java

public static Date geLastRequest(Context context, PublicScript script, String trollId) {

    MhDlaSQLHelper helper = new MhDlaSQLHelper(context);
    SQLiteDatabase database = helper.getReadableDatabase();

    Cursor cursor = database.rawQuery(SQL_LAST_REQUEST, new String[] { trollId, script.name() });
    Date result = null;//from   w w w .  j  a  v  a2  s. co m
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        long resultTimestamp = cursor.getLong(0);
        result = new Date(resultTimestamp);
    }

    cursor.close();
    database.close();

    String format = "Last request for category %s (script=%s) and troll=%s is: '%s'";
    String message = String.format(format, script.category, script, trollId, result);
    Log.d(TAG, message);

    return result;
}

From source file:org.zoumbox.mh_dla_notifier.sp.PublicScriptsProxy.java

public static Date geLastUpdate(Context context, PublicScript script, String trollId) {

    MhDlaSQLHelper helper = new MhDlaSQLHelper(context);
    SQLiteDatabase database = helper.getReadableDatabase();

    Cursor cursor = database.rawQuery(SQL_LAST_UPDATE,
            new String[] { trollId, script.name(), MhDlaSQLHelper.STATUS_SUCCESS });
    Date result = null;/*  w ww. java  2  s.  co  m*/
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        long resultTimestamp = cursor.getLong(0);
        result = new Date(resultTimestamp);
    }

    cursor.close();
    database.close();

    String format = "Last update for category %s (script=%s) and troll=%s is: '%s'";
    String message = String.format(format, script.category, script, trollId, result);
    Log.d(TAG, message);

    return result;
}

From source file:org.zoumbox.mh_dla_notifier.sp.PublicScriptsProxy.java

public static List<MhSpRequest> listLatestRequests(Context context, String trollId, int count) {
    List<MhSpRequest> result = new ArrayList<MhSpRequest>();

    String query = String.format(SQL_LIST_REQUESTS, count);

    MhDlaSQLHelper helper = new MhDlaSQLHelper(context);
    SQLiteDatabase database = helper.getReadableDatabase();

    Calendar calendar = Calendar.getInstance();

    Cursor cursor = database.rawQuery(query, new String[] { trollId });
    while (cursor.moveToNext()) {
        long startTimeMillis = cursor.getLong(0);
        long endTimeMillis = cursor.getLong(1);
        String scriptName = cursor.getString(2);
        String status = cursor.getString(3);

        calendar.setTimeInMillis(startTimeMillis);
        Date date = calendar.getTime();
        PublicScript script = PublicScript.valueOf(scriptName);

        long duration = 0;
        if (endTimeMillis > 0) {
            duration = endTimeMillis - startTimeMillis;
        }//from   w  w w  .  j  a  v a 2 s  .  co  m
        MhSpRequest request = new MhSpRequest(date, duration, script, status);
        result.add(request);
    }

    cursor.close();
    database.close();

    return result;
}

From source file:org.zoumbox.mh_dla_notifier.sp.PublicScriptsProxy.java

public static List<MhSpRequest> listLatestRequestsSince(Context context, String trollId, int dayCount) {
    List<MhSpRequest> result = new ArrayList<MhSpRequest>();

    Calendar instance = Calendar.getInstance();
    instance.add(Calendar.HOUR_OF_DAY, dayCount * -24);
    Date sinceDate = instance.getTime();

    MhDlaSQLHelper helper = new MhDlaSQLHelper(context);
    SQLiteDatabase database = helper.getReadableDatabase();

    Calendar calendar = Calendar.getInstance();

    Cursor cursor = database.rawQuery(SQL_LIST_REQUESTS_SINCE,
            new String[] { trollId, "" + sinceDate.getTime() });
    while (cursor.moveToNext()) {
        long startTimeMillis = cursor.getLong(0);
        long endTimeMillis = cursor.getLong(1);
        String scriptName = cursor.getString(2);
        String status = cursor.getString(3);

        calendar.setTimeInMillis(startTimeMillis);
        Date date = calendar.getTime();
        PublicScript script = PublicScript.valueOf(scriptName);

        long duration = 0;
        if (endTimeMillis > 0) {
            duration = endTimeMillis - startTimeMillis;
        }//from www.j a  v a 2s. c  o m
        MhSpRequest request = new MhSpRequest(date, duration, script, status);
        result.add(request);
    }

    cursor.close();
    database.close();

    return result;
}

From source file:org.zoumbox.mh_dla_notifier.sp.PublicScriptsProxy.java

public static Map<PublicScript, Date> geLastUpdates(Context context, String trollId,
        Set<PublicScript> scripts) {

    MhDlaSQLHelper helper = new MhDlaSQLHelper(context);
    SQLiteDatabase database = helper.getReadableDatabase();

    Map<PublicScript, Date> result = new HashMap<PublicScript, Date>();

    for (PublicScript script : scripts) {

        Cursor cursor = database.rawQuery(SQL_LAST_UPDATE,
                new String[] { trollId, script.name(), MhDlaSQLHelper.STATUS_SUCCESS });
        Date lastUpdate = null;/*from w  w w.ja  va2  s. co  m*/
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            long resultTimestamp = cursor.getLong(0);
            lastUpdate = new Date(resultTimestamp);
        }

        result.put(script, lastUpdate);
        cursor.close();
    }
    database.close();

    String format = "Last updates for troll=%s are: '%s'";
    String message = String.format(format, trollId, result);
    Log.i(TAG, message);

    return result;
}

From source file:org.zoumbox.mh_dla_notifier.sp.PublicScriptsProxy.java

protected static int computeRequestCount(Context context, ScriptCategory category, String trollId) {

    MhDlaSQLHelper helper = new MhDlaSQLHelper(context);
    SQLiteDatabase database = helper.getReadableDatabase();

    Calendar instance = Calendar.getInstance();
    instance.add(Calendar.HOUR_OF_DAY, -24);
    Date sinceDate = instance.getTime();

    Cursor cursor = database.rawQuery(SQL_COUNT,
            new String[] { trollId, category.name(), "" + sinceDate.getTime() });
    int result = 0;
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();//from  ww  w  .j a  v  a  2s  .  co m
        result = cursor.getInt(0);
    }

    cursor.close();
    database.close();

    String format = "Quota for category %s and troll=%s since '%s' is: %d";
    String message = String.format(format, category, trollId, sinceDate, result);
    Log.d(TAG, message);

    return result;
}

From source file:Main.java

/**
 * Get specific row values from the database,
 * e.g. all years stored in the database or
 *      all months of a year stored in the database or
 *      all days in a month of a year stored in the database
 *
 * @param db/*from  www .j ava 2s  . co m*/
 *            pointer to database
 * @param requestField
 *            requested row from db
 *            "year" returns all year values found
 *            "month" returns all month values found in year <code>requestLimiterYear</code>
 *            "day" returns all day values found in month <code>requestLimiterMonth</code>
 *                                              and year <code>requestLimiterYear</code>
 * @param requestLimiterMonth
 *            limiter for request
 *            unused if requestField is "year"
 *            unused if requestField is "month"
 *            month if requestField is "day"
 * @param requestLimiterYear
 *            limiter for request
 *            unused if requestField is "year"
 *            year if requestField is "month"
 *            year if requestField is "day"
 *
 * @return <code>ArrayList<Integer></code>
 *            array list with all entries found
 */
public static ArrayList<Integer> getEntries(SQLiteDatabase db, String requestField, int requestLimiterMonth,
        int requestLimiterYear) {

    /** Array list holding the found values */
    ArrayList<Integer> returnArray = new ArrayList<>();

    /** Limiter for row search */
    String queryRequest = "select distinct " + requestField + " from " + TABLE_NAME;
    if (requestField.equalsIgnoreCase("day")) {
        queryRequest += " where month = " + String.valueOf(requestLimiterMonth) + " and year = "
                + String.valueOf(requestLimiterYear);
    } else if (requestField.equalsIgnoreCase("month")) {
        queryRequest += " where year = " + String.valueOf(requestLimiterYear);
    }

    /** Cursor holding the records of a day */
    Cursor allRows = db.rawQuery(queryRequest, null);

    allRows.moveToFirst();
    for (int i = 0; i < allRows.getCount(); i++) {
        returnArray.add(allRows.getInt(0));
        allRows.moveToNext();
    }
    allRows.close();
    return returnArray;
}

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

public static boolean isAuthorFavorite(String author, Context context) {

    DBHelper dbhelper = new DBHelper(context);
    SQLiteDatabase dbread = dbhelper.getReadableDatabase();

    Cursor c = dbread.rawQuery("SELECT _id FROM favorite_users WHERE name=" + esc(author), null);

    boolean res = (c.getCount() > 0);

    c.close();//from  w ww  . j  a v  a 2  s  .c  om
    dbread.close();
    dbhelper.close();
    return res;
}

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

public static int getGroupUnreadCount(String group, Context context) {
    int groupid = getGroupIdFromName(group, context);

    DBHelper dbhelper = new DBHelper(context);
    SQLiteDatabase dbread = dbhelper.getReadableDatabase();

    Cursor c = dbread.rawQuery("SELECT _id FROM headers WHERE read=0 AND subscribed_group_id=" + groupid, null);
    int result = c.getCount();

    c.close();/*  w ww.ja v a2  s  .c om*/
    dbread.close();
    dbhelper.close();
    return result;
}