how to insert data in sqlite database and display in android
activity_main.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#abc" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:maxLines="1"
android:hint="Name"
android:layout_marginTop="28dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:hint="Sur Name"
android:maxLines="1"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_alignRight="@+id/editText2"
android:layout_below="@+id/editText2"
android:text="Insert Values"
android:onClick="insert"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:onClick="display"
android:text="Display all Values" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="20sp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity {
SQLiteDatabase db;
SQLiteDatabase db;
TextView tv;
EditText et1,et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize all view objects
tv=(TextView)findViewById(R.id.textView1);
et1=(EditText)findViewById(R.id.editText1);
et2=(EditText)findViewById(R.id.editText2);
//create database if not already exist
db= openOrCreateDatabase("Mydb", MODE_PRIVATE, null);
//create new table if not already exist
db.execSQL("create table if not exists mytable(name varchar, sur_name varchar)");
}
//This method will call on when we click on insert button
public void insert(View v)
{
String name=et1.getText().toString();
String sur_name=et2.getText().toString();
et1.setText("");
et2.setText("");
//insert data into able
db.execSQL("insert into mytable values('"+name+"','"+sur_name+"')");
//display Toast
Toast.makeText(this, "values inserted successfully.", Toast.LENGTH_LONG).show();
}
//This method will call when we click on display button
public void display(View v)
{
//use cursor to keep all data
//cursor can keep data of any data type
Cursor c=db.rawQuery("select * from mytable", null);
tv.setText("");
//move cursor to first position
c.moveToFirst();
//fetch all data one by one
do
{
//we can use c.getString(0) here
//or we can get data using column index
String name=c.getString(c.getColumnIndex("name"));
String surname=c.getString(1);
//display on text view
tv.append("Name:"+name+" and SurName:"+surname+"\n");
//move next position until end of the data
}while(c.moveToNext());
}
}
Comments
Post a Comment