This tutorial helps to store a data in sqlitedatabase. Android provides different path to store user data. SQlite is used for storing relevant data from user. Here i am explaining how to handle the data with sqlite operation.


1.Following is the structure of table



2.Create EditText 


We need to create Edittext for retrieving user information. Here the code that will show how to create edittext box.

EditText ed1,ed2;


3.Create Button


Now we need to create two button for handling database insert and view operation. Here we are implementing following code.

Button b1,b2;
SQLiteDatabase sq;


4.SQlite Database Handler


We need to write our class file to handle database (insert,view) operation.
1. Create a new project by going to File ⇒ New Android Project.
2. Once the project is created, create a new class in your project src directory and name it as DatabaseHandler.java ( Right Click on src/package ⇒ New ⇒ Class)
3. Now extend your SqliteActivity.java.

public class SqliteActivity extends Activity

4.After extending our class file we need to create database like below

sq=SqliteActivity.this.openOrCreateDatabase("company", MODE_PRIVATE, null);


5.Insert New Record


We need to insert a row value using execSQL query. 
    
b1.setOnClickListener(new OnClickListener() 
{
public void onClick(View arg0) 
{
{
try
{
sq.execSQL("create table com(names varchar2(20),city varchar2(20));");
String na=ed1.getText().toString();
String ci=ed2.getText().toString();
sq.execSQL("insert into com values('"+ na +"','"+ ci +"');");
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), "error"+e, Toast.LENGTH_SHORT).show();
}}
}
});


6.View Record


Suppose you need to view detail from record we use rawQuery for selecting the datbase. After selecting database the required fields are displayed.

b2.setOnClickListener(new OnClickListener() 
{
public void onClick(View arg0) 
{
{
try
{
Cursor c=sq.rawQuery("select *from com", null);
if(c!=null)
{
if(c.moveToFirst())
{
do
{
String n=c.getString(c.getColumnIndex("names"));
String c1=c.getString(c.getColumnIndex("city"));
Toast.makeText(getBaseContext(), "name = " + n + "city = " + c1  , Toast.LENGTH_SHORT).show();
}
while(c.moveToNext());
}
}
}
catch (Exception e) 
{
Toast.makeText(getBaseContext(), "error"+e, Toast.LENGTH_SHORT).show();
}
}  
}});
}
}


7.Designing Screen



Now we have developer the class file next thing we have to build screen for user interaction. we need to create one screen for storing and viewing the data from sqlite datbase.


Main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="load" />
</LinearLayout>


8.AndroidManifest


Dont forget to update you androidmanifest.xml file. 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sqlite"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".SqliteActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>


9.Make sure the above file you have placed in following image



10.Final Output


Download Code

Categories:

One Response so far.

  1. Unknown says:

    Congratulation for your blog. It helped me. I have a question, hoping you could give a hand. I want to have a server on a private cloud to see all my SQLite data on it, so many devices could see and modify the data. Please , help me :(