I am new bee in ANDROID so am getting problem in retrieving data especially a particular column from SQLite ,can anyone HELP me to know how it is possible.
1 Answer
Retrieving data from SQLite databases in Android is done using Cursors. The Android SQLite query method returns a Cursor object containing the results of the query. To use Cursors android.database.Cursor must be imported.
To get all the column values
Try this
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 a particular column data
Try this,
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 for the values like
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(); Hope this solves your problem.
3 Comments
Pradeep Menon
ThankYou verymuch for your answer.Note: I have a database having 28 rows just for saving app data. My goal is to read One specific column (not one value) from it. However, I don't want to read all columns at once, because it's slow as I have blob's in db too. Instead, I'd like to read just one column at a time, separately. Can I Use the code what you posted for me. Please reply.
Krishna
Yes definately, in the last code snippet you can see that cur.getString(colIndex) retrieves the value of a particular column in the loop. Column index defines the column no in your select query. Each iteration of the loop indicate your no of rows.
Pradeep Menon
Ok now , How can I display the retrieved column data from sqlite in listview form? Please reply