How can I get a specific coloumn from SQLite in android?

I am a new bee in ANDROID, so I get a problem with getting data, especially a specific column from SQLite, can someone HELP me to know how this is possible.

+3
source share
1 answer

Retrieving data from SQLite databases in Android is done using cursors. The SQL method SQLite query returns a Cursor object containing the query results. To use cursors android.database.Cursor needs to be imported.

To get all column values

try it

DatabaseHelper mDbHelper = new DatabaseHelper(getApplicationContext());

SQLiteDatabase mDb = mDbHelper.getWritableDatabase();

Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
            KEY_DESIGNATION}, null, null, null, null, null);

To get column data

Try it,

Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
              KEY_NAME, KEY_DESIGNATION}, KEY_ROWID + "=" + yourPrimaryKey, null,
              null, null, null, null);
        if (mCursor != null) {
          mCursor.moveToFirst();
        }

After getting the cursor, you can just iterate over the values, for example

cur.moveToFirst(); // move your cursor to first row
// Loop through the cursor
        while (cur.isAfterLast() == false) {
             cur.getString(colIndex); // will fetch you the data
            cur.moveToNext();
        }

    cur.close();

, .

+5

All Articles