Get the id of the first row whose column matches conditions
Description
The following code shows how to get the id of the first row whose column matches conditions.
Example
import org.xmlpull.v1.XmlPullParser;
//from w w w .ja v a2 s . c o m
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class DbUtils {
private SQLiteDatabase mDb;
private Context mContext;
private static final String TAG = DbUtils.class.getSimpleName();
/** Get the id of the first row whose column matches conditions **/
public static long getId(SQLiteDatabase db, String table, String column, String toMatch) {
String[] selectionArgs = new String[] { toMatch };
Cursor cursor = db.rawQuery("SELECT id FROM " + table + " where " + column + "=? LIMIT 1", selectionArgs);
long id = -1;
if (cursor.moveToFirst()) {
id = cursor.getLong(cursor.getColumnIndex("id"));
}
cursor.close();
return id;
}
}