0% found this document useful (0 votes)
11 views3 pages

Databaseproject Main Activity

The document describes creating an Android application that allows users to add, modify, delete and view data stored in a SQLite database table called Student. It includes code to define database and table schemas, connect buttons to trigger CRUD operations, and display messages on results.

Uploaded by

abeni mesfin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Databaseproject Main Activity

The document describes creating an Android application that allows users to add, modify, delete and view data stored in a SQLite database table called Student. It includes code to define database and table schemas, connect buttons to trigger CRUD operations, and display messages on results.

Uploaded by

abeni mesfin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package com.example.dell.

databaseprojectt;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements


android.view.View.OnClickListener {
SQLiteDatabase db;
EditText editsearchname,editstdname,editstdsec,editstdgrade,editstdepartment;
Button Add, Delete, Modify, View,search ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create database,StudentDB database name
db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
//create table Student
db.execSQL("CREATE TABLE IF NOT EXISTS Student(StdId INTEGER PRIMARY KEY
AUTOINCREMENT,StdName VARCHAR,StdSection VARCHAR,StdGrade
VARCHAR,StdDepartment);");
editsearchname = (EditText) findViewById(R.id.editstdname);
editstdname = (EditText) findViewById(R.id.editText);
editstdsec = (EditText) findViewById(R.id.editText2);
editstdgrade = (EditText) findViewById(R.id.editText3);
editstdepartment=(EditText) findViewById(R.id.editText4);

Add = (Button) findViewById(R.id.btnsave);


Delete= (Button) findViewById(R.id.btndel);
Modify= (Button) findViewById(R.id.btnupdate);
View= (Button) findViewById(R.id. btnselect);
search=(Button) findViewById(R.id. btnselectperticular);
Add.setOnClickListener(this);
Delete.setOnClickListener(this);
Modify.setOnClickListener(this);
View.setOnClickListener(this);
search.setOnClickListener(this);
}

public void msg(Context context,String str)


{
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}

@Override
public void onClick(android.view.View v) {
if(v.getId()==R.id.btnsave)
{
// code for save data
if(editstdname.getText().toString().trim().length()==0||
editstdsec.getText().toString().trim().length()==0||
editstdgrade.getText().toString().trim().length()==0)
{
msg(this, "Please enter all values");
return;
}
db.execSQL("INSERT INTO
Student(StdName,StdSection,StdGrade,StdDepartment)VALUES('"+ editstdname.getText()
+"','"+ editstdsec.getText()+ "','"+ editstdgrade.getText()
+"','"+editstdepartment.getText()+"');");
msg(this, "Record added");
}

else if(v.getId()==R.id.btnupdate)
{
//code for update data
if(editsearchname.getText().toString().trim().length()==0)
{
msg(this, "Enter Student Name");
return;
}
Cursor c=db.rawQuery("SELECT * FROM Student WHERE StdName='"+
editsearchname.getText()+"'", null);
if(c.moveToFirst()) {
db.execSQL("UPDATE Student SET StdName ='"+ editstdname.getText()
+"', StdSection='"+ editstdsec.getText()+"',StdGrade='"+
editstdgrade.getText()+"',StdDepartment='"+ editstdepartment.getText()+"' WHERE
StdName ='"+editsearchname.getText()+"'");
msg(this, "Record Modified");
}
else
{
msg(this, "Invalid Student Name");
}
}
else if(v.getId()==R.id.btndel)
{
//code for delete data
if(editsearchname.getText().toString().trim().length()==0)
{
msg(this, " Please enter Student Name ");
return;
}
Cursor c=db.rawQuery("SELECT * FROM Student WHERE StdName ='"+
editsearchname.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM Student WHERE StdName ='"+
editsearchname.getText()+"'");
msg(this, "Record Deleted");
}
else
{
msg(this, "Invalid Student Name ");
}
}
else if (v.getId() == R.id.btnselect)
{
//code for select all data
Cursor c=db.rawQuery("SELECT * FROM Student", null);
if(c.getCount()==0)
{
msg(this, "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("Student Name: "+c.getString(1)+"\n");
buffer.append("Student Section: "+c.getString(2)+"\n\n");
buffer.append("Student Grade: "+c.getString(3)+"\n\n");
buffer.append("Student Department: "+c.getString(4)+"\n\n");

}
msg(this, buffer.toString());
}
else if(v.getId()==R.id.btnselectperticular)
{
//code for select particular data
if(editsearchname.getText().toString().trim().length()==0)
{
msg(this, "Enter Student Name");
return;
}
Cursor c=db.rawQuery("SELECT * FROM Student WHERE
StdName='"+editsearchname.getText()+"'", null);
if(c.moveToFirst())
{
editstdname.setText(c.getString(1));
editstdsec.setText(c.getString(2));
editstdgrade.setText(c.getString(3));
editstdepartment.setText(c.getString(4));

}
else
{
msg(this, "Invalid Student Name");
}
}
}
}

You might also like