Example usage for android.database Cursor getInt

List of usage examples for android.database Cursor getInt

Introduction

In this page you can find the example usage for android.database Cursor getInt.

Prototype

int getInt(int columnIndex);

Source Link

Document

Returns the value of the requested column as an int.

Usage

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public ResponseFilter getResponseFilter(long id) {
    SQLiteDatabase db = this.getReadableDatabase();

    String selectQuery = "SELECT  * FROM " + TABLE_RESPONSE_FILTERS + " WHERE " + KEY_ID + " = " + id;

    Cursor c = db.rawQuery(selectQuery, null);

    if (c != null)
        c.moveToFirst();//from  w  w w  . jav a2s. co  m

    ResponseFilter responseFilter = new ResponseFilter();
    responseFilter.setId(c.getInt(c.getColumnIndex(KEY_ID)));
    responseFilter.setContent((c.getString(c.getColumnIndex(KEY_CONTENT))));
    responseFilter.setSource((c.getString(c.getColumnIndex(KEY_SOURCE))));
    responseFilter.setModified(c.getString(c.getColumnIndex(KEY_MODIFIED)));

    return responseFilter;
}

From source file:com.nnm.smsviet.Message.java

/**
 * Fetch MMS parts./*  ww w .j  av  a  2 s.  c o m*/
 * 
 * @param context
 *            {@link Context}
 */
private void fetchMmsParts(final Context context) {
    final ContentResolver cr = context.getContentResolver();
    Cursor cursor = cr.query(URI_PARTS, null, PROJECTION_PARTS[INDEX_MID] + " = ?",
            new String[] { String.valueOf(this.id) }, null);
    if (cursor == null || !cursor.moveToFirst()) {
        return;
    }
    final int iID = cursor.getColumnIndex(PROJECTION_PARTS[INDEX_ID]);
    final int iCT = cursor.getColumnIndex(PROJECTION_PARTS[INDEX_CT]);
    final int iText = cursor.getColumnIndex("text");
    do {
        final int pid = cursor.getInt(iID);
        final String ct = cursor.getString(iCT);
        Log.d(TAG, "part: " + pid + " " + ct);

        // get part
        InputStream is = null;

        final Uri uri = ContentUris.withAppendedId(URI_PARTS, pid);
        try {
            is = cr.openInputStream(uri);
        } catch (IOException e) {
            Log.e(TAG, "Failed to load part data", e);
        }
        if (is == null) {
            Log.i(TAG, "InputStream for part " + pid + " is null");
            if (iText >= 0 && ct.startsWith("text/")) {
                this.body = cursor.getString(iText);
            }
            continue;
        }
        if (ct == null) {
            continue;
        }
        if (ct.startsWith("image/")) {
            this.picture = BitmapFactory.decodeStream(is);
            final Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(uri, ct);
            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            this.contentIntent = i;
            continue; // skip the rest
        } else if (ct.startsWith("video/") || ct.startsWith("audio/")) {
            this.picture = BITMAP_PLAY;
            final Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(uri, ct);
            this.contentIntent = i;
            continue; // skip the rest
        } else if (ct.startsWith("text/")) {
            this.body = this.fetchPart(is);
        }

        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                Log.e(TAG, "Failed to close stream", e);
            } // Ignore
        }
    } while (cursor.moveToNext());
}

From source file:com.example.xiaoma.myapplication.model.DoHttpPostJson.java

/**
 * Post????Json?/*from  www.  j a  v a  2  s.co  m*/
 * BMS1?2
 */
@Override
public void carattribute(final Context context, final String url) {
    new Thread() {
        @Override
        public void run() {
            final String Url = url;
            Context mContext = context;
            myDBHelper = new MyDBOpenHelper(mContext, "db_ebike", null, 1);
            db = myDBHelper.getWritableDatabase();
            /**
             * ??JsonJSONObject
             */
            String licensenumber = ""; // ?
            String bms_hardwareversion = ""; // BMS?
            String bms_softwareversion = ""; // BMS?
            int cell_cap_rated = 0; // ??
            String cell_type = ""; // 
            int cell_box_nums = 0; // 
            int cell_serial_nums = 0; // 
            double volalarmhigh = 0; // ?
            double volcutoffhigh = 0; // ?
            double volalarmlow = 0; // 
            double volcutofflow = 0; // 
            double volalarmhigh_single = 0; // ??
            double volcutoffhigh_single = 0; // ??
            double volalarmlow_single = 0; // ?
            double volcutofflow_single = 0; // ?
            double tmpalarmhigh = 0; // ?
            double tmpcutoffhigh = 0; // ?
            double tmpalarmlow = 0; // ?
            double tmpcutofflow = 0; // ?
            double in_elealarmhigh = 0; // ?
            double in_elecutoffhigh = 0; // ?
            double out_elealarmhigh = 0; // ?
            double out_elecutoffhigh = 0; // ?
            double voldiff_alarmhigh = 0; // 
            double voldiff_cutoffhigh = 0; // 
            JSONObject jsonObj = new JSONObject();
            Cursor cursor = db.query("tb_carattribute", null, null, null, null, null, null);
            if (cursor.moveToFirst()) {
                do {
                    //int id = cursor.getInt(cursor.getColumnIndex("id"));
                    licensenumber = cursor.getString(cursor.getColumnIndex("licensenumber"));
                    bms_hardwareversion = cursor.getString(cursor.getColumnIndex("bms_hardwareversion"));
                    bms_softwareversion = cursor.getString(cursor.getColumnIndex("bms_softwareversion"));
                    cell_cap_rated = cursor.getInt(cursor.getColumnIndex("cell_cap_rated"));
                    cell_type = cursor.getString(cursor.getColumnIndex("cell_type"));
                    cell_box_nums = cursor.getInt(cursor.getColumnIndex("cell_box_nums"));
                    cell_serial_nums = cursor.getInt(cursor.getColumnIndex("cell_serial_nums"));
                    volalarmhigh = cursor.getDouble(cursor.getColumnIndex("volalarmhigh"));
                    volcutoffhigh = cursor.getDouble(cursor.getColumnIndex("volcutoffhigh"));
                    volalarmlow = cursor.getDouble(cursor.getColumnIndex("volalarmlow"));
                    volcutofflow = cursor.getDouble(cursor.getColumnIndex("volcutofflow"));
                    volalarmhigh_single = cursor.getDouble(cursor.getColumnIndex("volalarmhigh_single"));
                    volcutoffhigh_single = cursor.getDouble(cursor.getColumnIndex("volcutoffhigh_single"));
                    volalarmlow_single = cursor.getDouble(cursor.getColumnIndex("volalarmlow_single"));
                    volcutofflow_single = cursor.getDouble(cursor.getColumnIndex("volcutofflow_single"));
                    tmpalarmhigh = cursor.getDouble(cursor.getColumnIndex("tmpalarmhigh"));
                    tmpcutoffhigh = cursor.getDouble(cursor.getColumnIndex("tmpcutoffhigh"));
                    tmpalarmlow = cursor.getDouble(cursor.getColumnIndex("tmpalarmlow"));
                    tmpcutofflow = cursor.getDouble(cursor.getColumnIndex("tmpcutofflow"));
                    in_elealarmhigh = cursor.getDouble(cursor.getColumnIndex("in_elealarmhigh"));
                    in_elecutoffhigh = cursor.getDouble(cursor.getColumnIndex("in_elecutoffhigh"));
                    out_elealarmhigh = cursor.getDouble(cursor.getColumnIndex("out_elealarmhigh"));
                    out_elecutoffhigh = cursor.getDouble(cursor.getColumnIndex("out_elecutoffhigh"));
                    voldiff_alarmhigh = cursor.getDouble(cursor.getColumnIndex("voldiff_alarmhigh"));
                    voldiff_cutoffhigh = cursor.getDouble(cursor.getColumnIndex("voldiff_cutoffhigh"));
                    try {
                        jsonObj.put("licensenumber", licensenumber)
                                .put("bms_hardwareversion", bms_hardwareversion)
                                .put("bms_softwareversion", bms_softwareversion)
                                .put("cell_cap_rated", cell_cap_rated).put("cell_type", cell_type)
                                .put("cell_box_nums", cell_box_nums).put("cell_serial_nums", cell_serial_nums)
                                .put("volalarmhigh", volalarmhigh).put("volcutoffhigh", volcutoffhigh)
                                .put("volalarmlow", volalarmlow).put("volcutofflow", volcutofflow)
                                .put("volalarmhigh_single", volalarmhigh_single)
                                .put("volcutoffhigh_single", volcutoffhigh_single)
                                .put("volalarmlow_single", volalarmlow_single)
                                .put("volcutofflow_single", volcutofflow_single)
                                .put("tmpalarmhigh", tmpalarmhigh).put("tmpcutoffhigh", tmpcutoffhigh)
                                .put("tmpalarmlow", tmpalarmlow).put("tmpcutofflow", tmpcutofflow)
                                .put("in_elealarmhigh", in_elealarmhigh)
                                .put("in_elecutoffhigh", in_elecutoffhigh)
                                .put("out_elealarmhigh", out_elealarmhigh)
                                .put("out_elecutoffhigh", out_elecutoffhigh)
                                .put("voldiff_alarmhigh", voldiff_alarmhigh)
                                .put("voldiff_cutoffhigh", voldiff_cutoffhigh);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } while (cursor.moveToNext());
            }
            cursor.close();
            String jsonString = jsonObj.toString();

            // Post?
            List<NameValuePair> nameValuePairs = new ArrayList<>();
            nameValuePairs.add(new BasicNameValuePair("data", jsonString));

            try {
                //?HTTP
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(Url);
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                System.out.println("executing request " + httpPost.getURI());

                HttpResponse httpResponse = httpClient.execute(httpPost);
                try {
                    HttpEntity httpEntity = httpResponse.getEntity();
                    if (httpEntity != null) {
                        System.out.println("--------------------------------------");
                        System.out.println("Response content: " + EntityUtils.toString(httpEntity, "UTF-8"));
                        System.out.println("--------------------------------------");
                    }
                } finally {
                    System.out.println("--------------------------------------");
                }
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }.start();
}

From source file:com.textuality.lifesaver2.Columns.java

public JSONObject cursorToJSON(Cursor cursor) {
    setColumns(cursor);/*  www . j a  v a  2  s . c o  m*/
    JSONObject json = new JSONObject();
    try {
        for (int i = 0; i < mNames.length; i++) {
            int col = mColumns[i];
            if (cursor.isNull(col))
                continue;
            switch (mTypes[i]) {
            case STRING:
                String str = cursor.getString(col);
                if (mNames[i].equals("number")) {
                    json.put("name", mNameFinder.find(str));
                } else if (mNames[i].equals("address")) {
                    str = PhoneNumberUtils.formatNumber(str);
                    str = PhoneNumberUtils.stripSeparators(str);
                    json.put("name", mNameFinder.find(str));
                }
                json.put(mNames[i], str);
                break;
            case INT:
                json.put(mNames[i], cursor.getInt(col));
                break;
            case LONG:
                long val = cursor.getLong(col);
                json.put(mNames[i], val);
                if (mNames[i].equals("date")) {
                    json.put("tzoffset", mTZ.getOffset(val));
                }
                break;
            case FLOAT:
                json.put(mNames[i], cursor.getFloat(col));
                break;
            case DOUBLE:
                json.put(mNames[i], cursor.getDouble(col));
                break;
            }
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }

    return json;
}

From source file:com.cordova.photo.CameraLauncher.java

private int getImageOrientation(Uri uri) {
    int rotate = 0;
    String[] cols = { MediaStore.Images.Media.ORIENTATION };
    try {/*w ww.  java 2 s .co  m*/
        Cursor cursor = activity.getContentResolver().query(uri, cols, null, null, null);
        if (cursor != null) {
            cursor.moveToPosition(0);
            rotate = cursor.getInt(0);
            cursor.close();
        }
    } catch (Exception e) {
        // You can get an IllegalArgumentException if ContentProvider doesn't support querying for orientation.
    }
    return rotate;
}

From source file:com.hichinaschool.flashcards.libanki.sync.Syncer.java

/**
 * Deletions ********************************************************************
 *//*from   w ww. j  av  a2 s .  c  om*/

private JSONObject removed() {
    JSONArray cards = new JSONArray();
    JSONArray notes = new JSONArray();
    JSONArray decks = new JSONArray();
    Cursor cur = null;
    try {
        cur = mCol.getDb().getDatabase().rawQuery(
                "SELECT oid, type FROM graves WHERE usn" + (mCol.getServer() ? (" >= " + mMinUsn) : (" = -1")),
                null);
        while (cur.moveToNext()) {
            int type = cur.getInt(1);
            switch (type) {
            case Sched.REM_CARD:
                cards.put(cur.getLong(0));
                break;
            case Sched.REM_NOTE:
                notes.put(cur.getLong(0));
                break;
            case Sched.REM_DECK:
                decks.put(cur.getLong(0));
                break;
            }
        }
    } finally {
        if (cur != null && !cur.isClosed()) {
            cur.close();
        }
    }
    if (!mCol.getServer()) {
        mCol.getDb().execute("UPDATE graves SET usn=" + mMaxUsn + " WHERE usn=-1");
    }
    JSONObject o = new JSONObject();
    try {
        o.put("cards", cards);
        o.put("notes", notes);
        o.put("decks", decks);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    return o;
}

From source file:com.ichi2.libanki.Media.java

public Pair<String, Integer> syncInfo(String fname) {
    Cursor cur = null;
    try {/*from w  ww  .  j av  a2s . com*/
        cur = mDb.getDatabase().rawQuery("select csum, dirty from media where fname=?", new String[] { fname });
        if (cur.moveToNext()) {
            String csum = cur.getString(0);
            int dirty = cur.getInt(1);
            return new Pair<String, Integer>(csum, dirty);
        } else {
            return new Pair<String, Integer>(null, 0);
        }
    } finally {
        if (cur != null) {
            cur.close();
        }
    }
}

From source file:net.smart_json_database.JSONDatabase.java

private void updateTagMap() {
    String sql = "SELECT * FROM " + TABLE_TAG;
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor c = db.rawQuery(sql, new String[] {});
    if (c.getCount() > 0) {
        int col_id = c.getColumnIndex("tag_uid");
        int col_name = c.getColumnIndex("name");

        c.moveToFirst();//  w w w.j  a  v  a2s  .com
        do {
            tags.put(c.getString(col_name), new Integer(c.getInt(col_id)));
            invertedTags.put(new Integer(c.getInt(col_id)), c.getString(col_name));
        } while (c.moveToNext());
    }
    c.close();
    db.close();
}

From source file:com.ichi2.libanki.sync.Syncer.java

/**
 * Deletions ********************************************************************
 *//* w w w .ja  v  a  2  s  .  c  om*/

private JSONObject removed() {
    JSONArray cards = new JSONArray();
    JSONArray notes = new JSONArray();
    JSONArray decks = new JSONArray();
    Cursor cur = null;
    try {
        cur = mCol.getDb().getDatabase().rawQuery(
                "SELECT oid, type FROM graves WHERE usn" + (mCol.getServer() ? (" >= " + mMinUsn) : (" = -1")),
                null);
        while (cur.moveToNext()) {
            int type = cur.getInt(1);
            switch (type) {
            case Consts.REM_CARD:
                cards.put(cur.getLong(0));
                break;
            case Consts.REM_NOTE:
                notes.put(cur.getLong(0));
                break;
            case Consts.REM_DECK:
                decks.put(cur.getLong(0));
                break;
            }
        }
    } finally {
        if (cur != null && !cur.isClosed()) {
            cur.close();
        }
    }
    if (!mCol.getServer()) {
        mCol.getDb().execute("UPDATE graves SET usn=" + mMaxUsn + " WHERE usn=-1");
    }
    JSONObject o = new JSONObject();
    try {
        o.put("cards", cards);
        o.put("notes", notes);
        o.put("decks", decks);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    return o;
}

From source file:com.jsonstore.database.WritableDatabase.java

public int deleteIfRequired(JSONObject obj, boolean isErase, boolean exact) throws JSONException {
    boolean doErase = isErase; // start with the value that was passed in, override if necessary...
    boolean theEasyWay = true; // if there is an id, we have one object and can optimize the removal.
    int result = 0;
    int id = 0;//  w w  w .j  a  v  a 2  s .com
    String operation = null;

    try { // try the easy way first, check if there is a single id.
        id = obj.getInt(DatabaseConstants.FIELD_ID);
        operation = findOperationForObjectById(id);
    } catch (JSONException e) { // No ID was found, it either means no object found or the object didn't contain an ID field.
        theEasyWay = false; // Have to find the object the "hard" way (via query, using the info in obj) 
    }

    if (theEasyWay) {
        if ((operation != null) && (operation.equals(DatabaseConstants.OPERATION_ADD))) {
            doErase = true; // ...and force it to "true" if a non-pushed add/store is pending. Don't tag as "pending remove", just delete it.
        }

        if (doErase) {
            // We're going to immediately delete the objects.
            result = delete(new String[] { DatabaseConstants.FIELD_ID },
                    new Object[] { obj.getInt(DatabaseConstants.FIELD_ID) });
        } else {
            // We're not immediately removing the objects, so just mark them as dirty and update the operation to "remove".
            result = update(
                    new String[] { DatabaseConstants.FIELD_DELETED, DatabaseConstants.FIELD_DIRTY,
                            DatabaseConstants.FIELD_OPERATION },
                    new Object[] { 1, new Date().getTime(), DatabaseConstants.OPERATION_REMOVE }, id);
        }
    } else { // we have to do it the hard way, with a query and possibly multiple matches to loop through, some of which may be marked add, some not.
        String strOp;
        int intId;
        int actionCount = 0;
        Cursor removeSet = null;
        int setSize;

        // Use the JSONObject info to query a list of matches.
        removeSet = findUsingQueryObject(obj,
                new String[] { DatabaseConstants.FIELD_ID, DatabaseConstants.FIELD_OPERATION },
                new String[] { DatabaseConstants.FIELD_DELETED + " = 0" }, null, null, exact); //$NON-NLS-1$

        // Loop through the matches to separate the objects marked "add" from those marked !"add"
        setSize = removeSet.getCount();

        for (int i = 0; i < setSize; ++i) {
            JSONObject item = new JacksonSerializedJSONObject();

            removeSet.moveToNext();

            // Get the ID and Operation fields from each object in the set returned by the query.
            intId = removeSet.getInt(0);
            strOp = removeSet.getString(1);

            if ((DatabaseConstants.OPERATION_ADD.compareToIgnoreCase(strOp) == 0) || (isErase)) {
                // Actually delete all the matches marked "add" (count them up for return)
                result = delete(new String[] { DatabaseConstants.FIELD_ID }, new Object[] { intId });
            } else {
                // Actually update all the matches marked !"add" (count them up for return)
                result = update(
                        new String[] { DatabaseConstants.FIELD_DELETED, DatabaseConstants.FIELD_DIRTY,
                                DatabaseConstants.FIELD_OPERATION },
                        new Object[] { 1, new Date().getTime(), DatabaseConstants.OPERATION_REMOVE }, intId);
            }
            actionCount += result; // Keep a running tally of the total number of objects deleted/marked for delete.
        }

        result = actionCount;
        removeSet.close();
    }

    return result;
}