---
title: "Select Query in Android Application"  
description: "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 pr"  
author: "Chris Anderson"  
published: 2011-10-29  
updated: 2020-05-22  
canonical: https://www.mindstick.com/articles/797/select-query-in-android-application  
category: "android"  
tags: ["android"]  
reading_time: 4 minutes  

---

# 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"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
    <TextView android:id="@+id/txtID"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginTop="20px"
               android:layout_marginLeft="4px"
               android:textSize="30px" />
</LinearLayout>
```

##### · 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";

     @Override
     public void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         // create or open database file
         db = openOrCreateDatabase(database_name , SQLiteDatabase.CREATE_IF_NECESSARY,

                                                                                null);
         db.setVersion(1);
         db.setLocale(Locale.getDefault());
         db.setLockingEnabled(true);

         // creating table in database
         db.execSQL("CREATE TABLE IF NOT EXISTS "+table_name+" " +
                         "( sid INTEGER PRIMARY KEY AUTOINCREMENT," +
                         " name TEXT," +
                         " age INTEGER," +
                         " course TEXT ); ");

         //Insert("Rohit","19","CA");

         Cursor cur=fetchAllTodos();
         startManagingCursor(cur);

         cur.moveToFirst();
         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](https://www.mindstick.com/mindstickarticle/2643e627-4f5c-45cc-b166-9a9b4c9ff3b1/images/b9035d80-bc53-4196-a47a-0b1d94284d3d.png)

##### In my database the above records are present.

---

Original Source: https://www.mindstick.com/articles/797/select-query-in-android-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
