Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> Android >> Select query in Android Application
Select query in Android Application
Select query in Android Application


by Rohit Kesharwani on 10/29/2011 9:26:43 PM

Views: 6576       Comments: 1

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

In my database the above records are present.

Report Abuse Form
Reason:    
 


Confused
by Harvie Boles 10/23/2012 4:22:44 AM
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);                                                                        

    }

Report Abuse
Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Rohit KesharwaniRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 7411
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.