Home > DeveloperSection > Articles > Select Query in Android Application

Select Query in Android Application

Android Android 
1 Comment(s)
 21730  View(s)
Rate this:

Select Query in Android Application

In this article I am going to explain how to use select query in an Android application and fetching records from the cursor in Android.

·         Start a new project named SelectDemo.

·         Open res/layout/main.xml and insert the following:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"




    <TextView android:id="@+id/txtID"





               android:textSize="30px" />


·         Open the activity file and insert the following code:

import java.util.ArrayList;

import java.util.Locale;

import android.app.Activity;

import android.content.ContentValues;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.widget.TextView;

import android.widget.Toast;


public class SelectDemoActivity extends Activity {


       private SQLiteDatabase db;

       private String table_name="StudentInfo";

       private String database_name="Test.db";


       public static final String KEY_ROWID = "sid";

       public static final String KEY_NAME = "name";

       public static final String KEY_AGE = "age";

       public static final String KEY_COURSE = "course";



    public void onCreate(Bundle savedInstanceState)





        // create or open database file

        db = openOrCreateDatabase(database_name , SQLiteDatabase.CREATE_IF_NECESSARY, 





        // creating table in database

        db.execSQL("CREATE TABLE IF NOT EXISTS "+table_name+" " +

                        "( sid INTEGER PRIMARY KEY AUTOINCREMENT," +

                        "  name TEXT," +

                        "  age INTEGER," +

                        "  course TEXT ); ");




        Cursor cur=fetchAllTodos();




        new ArrayList<String>();

        do {

              TextView txtId=(TextView)findViewById(R.id.txtID);

              txtId.append(cur.getString(0)+"     "+cur.getString(1)+"    
+cur.getString(2)+"     "+cur.getString(3)+ "\n");

        }while (cur.moveToNext());



    // fetching records from database

    public Cursor fetchAllTodos()


              return db.query(table_name, new String[] { KEY_ROWID, KEY_NAME,

                          KEY_AGE, KEY_COURSE }, null, null, null,null, null);                                                                        



    // inserting record in the database

    public void Insert(String name, String age, String course)


       ContentValues data=createContentValues(name, age, course);

       db.insert(table_name, null, data);

       Toast.makeText(this, "Record Inserted", Toast.LENGTH_SHORT).show();



    // return a content of the database

    private ContentValues createContentValues(String name, String age, String course)


              ContentValues values = new ContentValues();

              values.put(KEY_NAME, name);

              values.put(KEY_AGE, age);

              values.put(KEY_COURSE, course);

              return values;



·         Run the application.

When you run the application, it will display all the records present in the table.

Select query in Android Application

In my database the above records are present.


By Harvie Boles on   4 years ago
what is the meaning of too many null ?

 public Cursor fetchAllTodos()


              return db.query(table_namenew String[] { KEY_ROWID, KEY_NAME,

                          KEY_AGEKEY_COURSE }, nullnullnull,null,null);                                                                        


Don't want to miss updates? Please click the below button!

Follow MindStick