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:com.acrylicgoat.devchat.MainActivity.java

private void getToday(String owner) {
    //Log.d("MainActivity", "getToday() called: " + owner);

    DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    cursor = db.rawQuery(getTodaySQL(), null);
    if (cursor.getCount() > 0) {
        cursor.moveToNext();// w w  w. j  av a  2 s.c  om
        int notesColumn = cursor.getColumnIndex(Notes.NOTE);
        today.setText(cursor.getString(notesColumn));

    } else {
        if (devs.size() > 0) {
            today.setText("");
        } else {
            today.setText(getString(R.string.no_devs));

        }

    }
    Linkify.addLinks(today, Linkify.ALL);
    cursor.close();
    db.close();
}

From source file:heartware.com.heartware_master.DBAdapter.java

public HashMap<String, String> getMeetupInfo(final String note, final String userId) {
    HashMap<String, String> workoutMap = new HashMap<String, String>();
    SQLiteDatabase database = this.getReadableDatabase();
    String selectQuery = "SELECT * FROM " + MEETUPS_TABLE + " WHERE " + NOTE + "='" + note + "'" + " AND "
            + USER_ID + " = " + userId;
    Cursor cursor = database.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {/* www .j a  v  a2s .  c om*/
            workoutMap.put(USER_ID, cursor.getString(0));
            workoutMap.put(NOTE, cursor.getString(1));
            workoutMap.put(EXERCISE, cursor.getString(2));
            workoutMap.put(LOCATION, cursor.getString(3));
            workoutMap.put(DATE, cursor.getString(4));
            workoutMap.put(PEOPLE, cursor.getString(5));
        } while (cursor.moveToNext());
    }
    return workoutMap;
}

From source file:com.liferay.alerts.database.DatabaseHelper.java

private void _convertMessageToJSONObject(SQLiteDatabase database) {
    StringBuilder select = new StringBuilder();

    select.append("SELECT ");
    select.append(Alert.ID);/*from  w  w w .  j ava  2s .c  o  m*/
    select.append(", ");
    select.append(Alert.PAYLOAD);
    select.append(" FROM ");
    select.append(AlertDAO.TABLE_NAME);

    Cursor cursor = database.rawQuery(select.toString(), null);

    while (cursor.moveToNext()) {
        try {
            long id = cursor.getLong(cursor.getColumnIndex(Alert.ID));
            String payload = cursor.getString(cursor.getColumnIndex(Alert.PAYLOAD));

            JSONObject jsonObject = new JSONObject();
            jsonObject.put(Alert.MESSAGE, payload);

            ContentValues values = new ContentValues();
            values.put(Alert.PAYLOAD, jsonObject.toString());

            StringBuilder sb = new StringBuilder();

            sb.append(Alert.ID);
            sb.append(CharPool.SPACE);
            sb.append("= ?");

            String whereClause = sb.toString();

            String[] whereArgs = { String.valueOf(id) };

            database.update(AlertDAO.TABLE_NAME, values, whereClause, whereArgs);
        } catch (JSONException je) {
            Log.e(_TAG, "Couldn't convert message.", je);
        }
    }
}

From source file:com.acrylicgoat.scrumnotes.MainActivity.java

private void getToday(String owner) {
    //Log.d("MainActivity", "getToday() called: " + owner);

    DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    cursor = db.rawQuery(getTodaySQL(), null);
    if (cursor.getCount() > 0) {
        cursor.moveToNext();//from  w w  w.j  a v  a  2 s.  c o  m
        int notesColumn = cursor.getColumnIndex(Notes.NOTE);
        today.setText(cursor.getString(notesColumn));

    } else {
        if (devs.size() > 0) {
            today.setText(getString(R.string.main_insert));
        } else {
            today.setText(getString(R.string.no_devs));
        }

    }
    Linkify.addLinks(today, Linkify.ALL);
    cursor.close();
    db.close();
}

From source file:org.rapidandroid.activity.chart.form.FormDataBroker.java

private JSONGraphData loadMessageOverTimeHistogram() {
    Date startDateToUse = getStartDate();
    DateDisplayTypes displayType = this.getDisplayType(startDateToUse, mEndDate);

    String selectionArg = getSelectionString(displayType);

    StringBuilder rawQuery = new StringBuilder();

    rawQuery.append("select time, count(*) from  ");
    rawQuery.append(RapidSmsDBConstants.FormData.TABLE_PREFIX + mForm.getPrefix());

    rawQuery.append(" join rapidandroid_message on (");
    rawQuery.append(RapidSmsDBConstants.FormData.TABLE_PREFIX + mForm.getPrefix());
    rawQuery.append(".message_id = rapidandroid_message._id");
    rawQuery.append(") ");
    if (startDateToUse.compareTo(Constants.NULLDATE) != 0 && mEndDate.compareTo(Constants.NULLDATE) != 0) {
        rawQuery.append(" WHERE rapidandroid_message.time > '" + Message.SQLDateFormatter.format(startDateToUse)
                + "' AND rapidandroid_message.time < '" + Message.SQLDateFormatter.format(mEndDate) + "' ");
    }/*from ww w.  j a va 2 s .co  m*/

    rawQuery.append(" group by ").append(selectionArg);
    rawQuery.append("order by ").append(selectionArg).append(" ASC");

    // the X date value is column 0
    // the y value magnitude is column 1
    SQLiteDatabase db = rawDB.getReadableDatabase();
    Cursor cr = db.rawQuery(rawQuery.toString(), null);
    return getDateQuery(displayType, cr, db);

}

From source file:org.jumpmind.symmetric.android.AndroidSqlTemplate.java

protected <T> T queryForObject(SQLiteDatabase database, String sql, Class<T> clazz, Object... params) {
    Cursor cursor = null;//from ww  w.  j a  v a2  s . c  o m
    try {
        T result = null;
        cursor = database.rawQuery(sql, toStringArray(params));
        if (cursor.moveToFirst()) {
            result = get(cursor, clazz, 0);
        }
        return result;
    } catch (Exception ex) {
        throw translate(ex);
    } finally {
        close(cursor);
    }
}

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;
    Cursor c = null;//from ww w . j  a v  a  2 s.c  om
    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.acrylicgoat.devchat.MainActivity.java

private void saveNote() {
    ContentValues values = new ContentValues();

    String text = today.getText().toString() + " ";
    //Log.d("NoteEditorActivity", "note: " + text);
    int length = text.length();

    if (length == 0 || text.contains("To get started, select Tools") || text.equals("") || text.equals(" ")) {
        //Toast.makeText(this, "Nothing to save.", Toast.LENGTH_SHORT).show();
        return;/*w  w  w.j  a  v a  2 s .c om*/
    }

    values.put(Notes.NOTE, text);
    values.put(Notes.OWNER, currentOwner);

    //check if a note already exists for today
    DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    cursor = db.rawQuery(getTodaySQL(), null);
    if (cursor.getCount() > 0) {
        //Log.d("MainActivity", "saveNote(): doing update ");
        StringBuilder sb = new StringBuilder();
        sb.append("update notes set notes_note = '");
        sb.append(escape(text));
        sb.append("' where notes_owner='");
        sb.append(currentOwner);
        sb.append("' and date(notes_date) = date('now','localtime')");
        dbHelper.getReadableDatabase().execSQL(sb.toString());
    } else {
        getContentResolver().insert(Notes.CONTENT_URI, values);
    }
    cursor.close();
    db.close();

}

From source file:heartware.com.heartware_master.DBAdapter.java

public HashMap<String, String> getProfileByUserAndToken(String user, String token) {
    HashMap<String, String> profileMap = new HashMap<String, String>();
    SQLiteDatabase database = this.getReadableDatabase();
    String selectQuery = "SELECT * FROM " + PROFILES_TABLE + " WHERE " + USERNAME + "='" + user + "'" + " AND "
            + PASSWORD + "='" + token + "'";
    Cursor cursor = database.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {//from w  w  w  . j a  v a2 s .  co  m
            profileMap.put(PROFILE_ID, cursor.getString(0));
            profileMap.put(USERNAME, cursor.getString(1));
            profileMap.put(PASSWORD, cursor.getString(2));
            profileMap.put(DIFFICULTY, cursor.getString(3));
            profileMap.put(DISABILITY, cursor.getString(4));

        } while (cursor.moveToNext());
    }
    return profileMap;
}

From source file:com.acrylicgoat.scrumnotes.MainActivity.java

private String getYesterday(String owner) {
    //Log.d("MainActivity", "getYesterday() called: " + owner);
    StringBuilder sb = new StringBuilder();
    String results = "";
    sb.append("select notes_note from notes where notes_owner='");
    sb.append(currentOwner);/*from w w  w .  j  a v a 2s .  c  o m*/

    Calendar calendar = new GregorianCalendar();
    int day = calendar.get(Calendar.DAY_OF_WEEK);
    if (day == 2) {
        //it is Monday so retrieve Friday
        sb.append("' and date(notes_date)=date('now','localtime','-3 day')");
    } else {
        //not Monday
        sb.append("' and date(notes_date)=date('now','localtime','-1 day')");
    }

    DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    cursor = db.rawQuery(sb.toString(), null);
    if (cursor.getCount() > 0) {
        cursor.moveToNext();
        int notesColumn = cursor.getColumnIndex(Notes.NOTE);
        //Log.d("MainActivity.getYesterday()", "notesColumn: " + notesColumn);
        results = cursor.getString(notesColumn);

    }

    cursor.close();
    db.close();
    return results;
}