Hello !!
For many other Language Programs of Computer Network, Mobile Computing, microprocessor 8086, C++ , Data Structures , etc and MSBTE diploma,BCA,MCA and engineering related concepts visit our BLOG
- Microprocessor
Data Structures
- C Language Program
- Nursing
- Android and Cloud Computing
- Computer Netwoks , Mobile Computing
package com.example.myapplication;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity
implements OnClickListener
{
EditText Rollno,Name,Marks; Button
Insert,Delete,Update,View,ViewAll;
SQLiteDatabase db;
@Override
public void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Rollno=(EditText)findViewById(R.id.Rollno)
;
Name=(EditText)findViewById(R.id.Name);
Marks=(EditText)findViewById(R.id.Marks);
Insert=(Button)findViewById(R.id.Insert);
Delete=(Button)findViewById(R.id.Delete);
Update=(Button)findViewById(R.id.Update
);
View=(Button)findViewById(R.id.View);
ViewAll=(Button)findViewById(R.id.ViewAll)
;
Insert.setOnClickListener(this);
Delete.setOnClickListener(this); Update.setOnClickListener(this);
View.setOnClickListener(this); ViewAll.setOnClickListener(this);
db=openOrCreateDatabase("StudentDB",
Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT
EXISTS student(rollno VARCHAR,name VARCHAR,marks
VARCHAR);");
}
public void onClick(View view)
{
if(view==Insert)
{
if(Rollno.getText().toString().trim().length()
==0|| Name.getText().toString().trim().length()== 0||
Marks.getText().toString().trim().length()== 0)
{
Rollno");
}
showMessage("Error", "Invalid
showMessage("Error",
"Please enter all values");
return;
}
db.execSQL("INSERT INTO
student VALUES('"+Rollno.getText()+"','"+Name.get Text()+
"','"+Marks.getText()+"');");
showMessage("Success", "Record
added");
clearText();
}
if(view==Delete)
{
if(Rollno.getText().toString().trim().length()
==0)
{
showMessage("Error",
"Please enter Rollno");
return;
}
Cursor
c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM
student WHERE rollno='"+Rollno.getText()+"'");
showMessage("Success",
"Record Deleted");
}
else
{
clearText();
}
if(view==Update)
{
if(Rollno.getText().toString().trim().length()
==0)
{
showMessage("Error",
"Please enter Rollno");
return;
}
Cursor
c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst()) {
db.execSQL("UPDATE student SET
name='" + Name.getText() + "',marks='" + Marks.getText() +
"' WHERE
rollno='"+Rollno.getText()+"'");
showMessage("Success", "Record
Modified");
}
else {
showMessage("Error", "Invalid
Rollno");
}
clearText();
if(view==View)
{
if(Rollno.getText().toString().trim().length()
==0)
{
showMessage("Error",
"Please enter Rollno");
return;
}
Cursor
c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst())
{
Name.setText(c.getString(1));
Marks.setText(c.getString(2));
}
else
{
showMessage("Error", "Invalid
Rollno");
clearText();
}
}
if(view==ViewAll)
{
Cursor
c=db.rawQuery("SELECT * FROM student", null);
if(c.getCount()==0)
{
showMessage("Error",
"No records found");
return;
}
StringBuffer buffer=new
StringBuffer();
while(c.moveToNext())
{
buffer.append("Rollno:
"+c.getString(0)+"\n");
buffer.append("Name:
"+c.getString(1)+"\n");
buffer.append("Marks:
"+c.getString(2)+"\n\n");
}
showMessage("Student
Details", buffer.toString());
}
}
public void
showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true); builder.setTitle(title);
builder.setMessage(message); builder.show();
}
public void clearText()
{
Rollno.setText("");
Name.setText("");
Marks.setText("");
Rollno.requestFocus();
}
}
XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.c om/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="82dp" android:layout_y="31dp"
android:text="Student Details" android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp" android:layout_y="110dp"
android:text="Enter Rollno:" android:textSize="20sp" />
<EditText
android:id="@+id/Rollno" android:layout_width="150dp"
android:layout_height="wrap_content" android:layout_x="175dp" android:layout_y="100dp" android:inputType="number"
android:textSize="20sp" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_x="20dp" android:layout_y="160dp" android:text="Enter Name:" android:textSize="20sp" />
<EditText android:id="@+id/Name" android:layout_width="150dp"
android:layout_height="wrap_content" android:layout_x="175dp" android:layout_y="150dp" android:inputType="text" android:textSize="20sp" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_x="20dp" android:layout_y="210dp" android:text="Enter Marks:" android:textSize="20sp" />
<EditText android:id="@+id/Marks" android:layout_width="150dp"
android:layout_height="wrap_content" android:layout_x="175dp" android:layout_y="200dp" android:inputType="number" android:textSize="20sp" />
<Button android:id="@+id/Insert" android:layout_width="346dp"
android:layout_height="wrap_content" android:layout_x="11dp" android:layout_y="300dp" android:text="Insert" android:textSize="30dp" />
<Button android:id="@+id/Delete" android:layout_width="348dp"
android:layout_height="wrap_content" android:layout_x="11dp" android:layout_y="377dp" android:text="Delete" android:textSize="30dp" />
<Button android:id="@+id/Update" android:layout_width="350dp"
android:layout_height="wrap_content" android:layout_x="10dp" android:layout_y="459dp" android:text="Update" android:textSize="30dp" />
<Button
android:id="@+id/View"
android:layout_width="350dp"
android:layout_height="wrap_content" android:layout_x="10dp" android:layout_y="532dp" android:text="View" android:textSize="30dp" />
<Button android:id="@+id/ViewAll" android:layout_width="355dp"
android:layout_height="wrap_content" android:layout_x="12dp" android:layout_y="609dp" android:text="View All" android:textSize="30dp" />
</AbsoluteLayout>
No comments:
Post a Comment