DreamsCoder.com

We Code your Dreams


home > How to retrieve values from SQlite Database




How to retrieve values from SQlite Database







In the previous we have learned

how to insert values into database in Android


This program in part of the previous program

Here we will learn how to fetch values from Database
In next Artilce we are goin to learn

how to check values inserted in SqliteDatabase - Click Here



package com.example.simpledatabase;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

DBhelper dbh;
SQLiteDatabase dtb; //to fire Queries

	@Override
	protected void onCreate(Bundle savedInstanceState) 
       {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
		
		DBhelper dbh = new DBhelper(this);
		dtb = dbh.getWritableDatabase();
		
	}
		
		Cursor cs=dtb.query("addrecord", 
new String[]
{
dbh.num,dbh.name}, null, null, 
null, null, null);
		if(cs!=null)
		{
			cs.moveToFirst();
		}
		int no;
		String nm;
		do
		{
		no = Integer.parseInt(cs.getString(0));
		nm = cs.getString(1);
	
		Toast.makeText(this, 
"Number is : "+no+" Name is :"+nm, Toast.LENGTH_LONG).show();

		}while(cs.moveToNext()); 
//while last record is present
		
	} // Cursor
}//class

Label - android





by - Chinmay Mahajan





Are you looking for something else?


Suggested Programs




Working with SQlite Database in Android


How to check values inserted in SqliteDatabase


Simple Android program to insert values into Database


Simple Text-to-Speech Android App


Error while Creating New Android Project in Eclipse