raw Fetch Id from database - Android Database

Android examples for Database:SQL Query

Description

raw Fetch Id from database

Demo Code

/*//from   w  ww . ja  v a  2s.  c  om
 * Copyright (c) 2012 Denis Solonenko.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */
import android.database.Cursor;

public class Main{
    public static long rawFetchId(DatabaseAdapter db, String query,
            String[] selectionArgs) {
        return rawFetchLong(db, query, selectionArgs, -1);
    }
    private static long rawFetchLong(DatabaseAdapter db, String query,
            String[] selectionArgs, long defaultValue) {
        Cursor c = db.db().rawQuery(query, selectionArgs);
        try {
            if (c.moveToFirst()) {
                return c.getLong(0);
            }
        } finally {
            c.close();
        }
        return defaultValue;
    }
}

Related Tutorials