Example usage for android.database Cursor close

List of usage examples for android.database Cursor close

Introduction

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

Prototype

void close();

Source Link

Document

Closes the Cursor, releasing all of its resources and making it completely invalid.

Usage

From source file:ro.weednet.contactssync.platform.ContactManager.java

private static long lookupContact(ContentResolver resolver, String name, String fb_id, boolean joinById,
        boolean syncAll) {
    Cursor c;

    if (joinById) {
        c = resolver.query(ContactsContract.Data.CONTENT_URI, //table
                new String[] { ContactsContract.Data.RAW_CONTACT_ID }, //select (projection)
                ContactsContract.Data.MIMETYPE + "=? AND " + CommonDataKinds.Note.NOTE + " LIKE ?", //where
                new String[] { ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE, "%" + fb_id + "%" }, //params
                null //sort
        );//from w  ww .  jav a2s .  c  o  m
        try {
            if (c != null && c.moveToFirst()) {
                return c.getLong(0);
            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

    if (syncAll) {
        return -1;
    }

    c = resolver.query(Contacts.CONTENT_URI, //table
            new String[] { Contacts._ID }, //select (projection)
            Contacts.DISPLAY_NAME + "=?", //where
            new String[] { name }, //params
            null //sort
    );
    try {
        if (c != null && c.getCount() > 0) {
            return 0;
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }

    return -1;
}

From source file:com.taxicop.sync.SyncAdapter.java

public int count(ContentProviderClient provider) {
    try {/* www. j a v  a 2s. co m*/
        Cursor c = provider.query(PlateContentProvider.URI_REPORT, null, null, null, null);
        int ret = c.getCount();
        c.close();
        return ret;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.e(TAG, "" + e.getMessage());
    }
    return 0;
}

From source file:net.peterkuterna.android.apps.devoxxfrsched.service.TwitterSearchService.java

@Override
protected void doSync(Intent intent) throws Exception {
    Log.d(TAG, "Refreshing twitter results");

    final Cursor c = mResolver.query(Tweets.CONTENT_URI, TweetsQuery.PROJECTION, null, null, null);

    String tweetId = "0";
    try {/*from  w w  w  . j a v a 2s.c  om*/
        if (c.moveToFirst()) {
            tweetId = c.getString(TweetsQuery.MAX_TWEET_ID);
        }
    } finally {
        c.close();
    }

    final Uri uri = TwitterApiUriUtils.buildTwitterSearchUri("#devoxxfr +exclude:retweets", tweetId);

    mRemoteExecutor.executeGet(uri.toString(), new TwitterSearchHandler());
}

From source file:net.peterkuterna.android.apps.devoxxsched.service.TwitterSearchService.java

@Override
protected void doSync(Intent intent) throws Exception {
    Log.d(TAG, "Refreshing twitter results");

    final Cursor c = mResolver.query(Tweets.CONTENT_URI, TweetsQuery.PROJECTION, null, null, null);

    String tweetId = "0";
    try {/*from   w  w w  .  j a  v a 2s. c  om*/
        if (c.moveToFirst()) {
            tweetId = c.getString(TweetsQuery.MAX_TWEET_ID);
        }
    } finally {
        c.close();
    }

    final Uri uri = TwitterApiUriUtils.buildTwitterSearchUri("from:Devoxx OR #devoxx +exclude:retweets",
            tweetId);

    mRemoteExecutor.executeGet(uri.toString(), new TwitterSearchHandler());
}

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

public static HashSet<String> getFavoriteAuthors(Context context) {

    HashSet<String> favoriteAuthors = null;

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

    Cursor c = dbread.rawQuery("SELECT name FROM favorite_users", null);
    if (c.getCount() > 0) {
        favoriteAuthors = new HashSet<String>(c.getCount());
        c.moveToFirst();/*from   w ww  .j av a  2  s .  c om*/

        int count = c.getCount();
        for (int i = 0; i < count; i++) {
            favoriteAuthors.add(c.getString(0));
            c.moveToNext();
        }
    }

    c.close();
    dbread.close();
    db.close();

    if (favoriteAuthors == null)
        favoriteAuthors = new HashSet<String>(0);
    return favoriteAuthors;
}

From source file:net.peterkuterna.android.apps.devoxxfrsched.service.NewsSyncService.java

@Override
protected void doSync(Intent intent) throws Exception {
    Log.d(TAG, "Refreshing DevoxxFr twitter results");

    boolean noNotifications = intent.getBooleanExtra(EXTRA_NO_NOTIFICATIONS, false);

    final Cursor c = mResolver.query(News.CONTENT_URI, NewsQuery.PROJECTION, null, null, null);

    String newsId = "0";
    try {/*w  w w  . j av a 2  s.  co m*/
        if (c.moveToFirst()) {
            newsId = c.getString(NewsQuery.MAX_NEWS_ID);
        }
    } finally {
        c.close();
    }

    final Uri uri = TwitterApiUriUtils.buildTwitterSearchUri("from:DevoxxFr +exclude:retweets", newsId);

    mRemoteExecutor.executeGet(uri.toString(), new NewsHandler(noNotifications));

    final NotifierManager notifierManager = new NotifierManager(this);
    notifierManager.notifyNewNewsItems();

    Log.d(TAG, "News sync finished");
}

From source file:com.phonegap.plugin.sqlitePlugin.SQLitePlugin.java

/**
 * Execute SQL statement.//from  www .ja  va  2  s.  c  om
 *
 * @param query
 *            The SQL query
 * @param params
 *            Parameters for the query
 * @param tx_id
 *            Transaction id
 */
public void executeSql(String query, String[] params, String tx_id) {
    String cmd = query.toLowerCase();

    try {
        if (cmd.startsWith("insert")) {
            SQLiteStatement myStatement = this.myDb.compileStatement(query);
            long insertId = myStatement.executeInsert();
            this.sendJavascript("dddb.completeQuery('" + tx_id + "', [{'insertId':'" + insertId
                    + "', 'rowsAffected':'1'}]);");
            /* Test fix for #22, requires API 11+: **
            } else if (cmd.startsWith("update") || cmd.startsWith("delete")) {
               SQLiteStatement myStatement = this.myDb.compileStatement(query);
               int rowsAffected = myStatement.executeUpdateDelete();
               this.sendJavascript("dddb.completeQuery('" + tx_id + "', [{'rowsAffected':'" + rowsAffected + "'}]);");
             * END of Test fix for #22, requires API 11+ */
        } else if (isDDL(query)) {
            this.myDb.execSQL(query);
            this.sendJavascript("dddb.completeQuery('" + tx_id + "', '');");
        } else {
            Cursor myCursor = this.myDb.rawQuery(query, params);
            this.processResults(myCursor, tx_id);
            myCursor.close();
        }
    } catch (SQLiteException ex) {
        ex.printStackTrace();
        System.out.println("SQLitePlugin.executeSql(): Error=" + ex.getMessage());

        // Send error message back to JavaScript
        this.sendJavascript("dddb.fail('" + ex.getMessage() + "','" + tx_id + "');");
    }
}

From source file:mil.nga.giat.mage.sdk.utils.MediaUtility.java

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context The context./*from   w w w.java  2 s  . c  o  m*/
 * @param uri The Uri to query.
 * @param selection (Optional) Filter used in the query.
 * @param selectionArgs (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 */
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {

            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } catch (Exception e) {
        Log.e(LOG_NAME, "Error getting data column", e);
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:com.phonegap.plugins.sqlitePlugin.SQLitePlugin.java

public void executeSqlBatch(String[] queryarr, String[][] paramsarr, String[] queryIDs, String tx_id) {
    try {/*from w  w  w  .  ja v a2s .  c om*/
        this.myDb.beginTransaction();
        String query = "";
        String query_id = "";
        String[] params;
        int len = queryarr.length;
        for (int i = 0; i < len; i++) {
            query = queryarr[i];
            params = paramsarr[i];
            query_id = queryIDs[i];
            Cursor myCursor = this.myDb.rawQuery(query, params);

            this.processResults(myCursor, query_id, tx_id);
            myCursor.close();
        }
        this.myDb.setTransactionSuccessful();
    } catch (SQLiteException ex) {
        ex.printStackTrace();
        Log.v("executeSqlBatch", "SQLitePlugin.executeSql(): Error=" + ex.getMessage());
        this.sendJavascript(
                "SQLitePluginTransaction.txErrorCallback('" + tx_id + "', '" + ex.getMessage() + "');");
    } finally {
        this.myDb.endTransaction();
        Log.v("executeSqlBatch", tx_id);
        this.sendJavascript("SQLitePluginTransaction.txCompleteCallback('" + tx_id + "');");
    }
}

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

public static Vector<Long> getPendingOutgoingMessageIds(Context context) {

    Vector<Long> retVal = null;
    DBHelper db = new DBHelper(context);
    SQLiteDatabase dbread = db.getReadableDatabase();

    Cursor c = dbread.rawQuery("SELECT _id FROM offline_sent_posts", null);
    int count = c.getCount();

    if (count == 0) {
        retVal = new Vector<Long>(0);
    } else {/* w  w w.  j  a va 2  s . c om*/
        retVal = new Vector<Long>(count);
        c.moveToFirst();

        for (int i = 0; i < count; i++) {
            retVal.add(c.getLong(0));
            c.moveToNext();
        }
    }

    c.close();
    dbread.close();
    db.close();
    return retVal;
}