MainActivity.java
package com.example.pc131.databasedymaic;
import android.content.Intent;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText e1 ,e2;
Button bi,bu,bd,bs;
SQLiteDatabase mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=findViewById(R.id.edit1);
e2=findViewById(R.id.edit2);
bi=findViewById(R.id.but1);
bu=findViewById(R.id.but2);
bd=findViewById(R.id.but3);
bs=findViewById(R.id.but4);
mydb=openOrCreateDatabase("mydb.db",0,null);
String createtbl="CREATE TABLE IF NOT EXISTS emp(id INTEGER PRIMARY KEY AUTOINCREMENT,FNAME TEXT,LNAME TEXT);";
mydb.execSQL(createtbl);
}
public void myinsert(View view) {
try
{
if(e1.getText().toString().trim().length()>0 && e2.getText().toString().trim().length()>0)
{
mydb.execSQL("insert into emp(fname,lname)values('"+e1.getText().toString()+"','"+e2.getText().toString()+"');");
Toast.makeText(this,"Records insert successfully",Toast.LENGTH_LONG).show();
e1.setText("");
e2.setText("");
}
else
{
Toast.makeText(this,"Enter the record",Toast.LENGTH_LONG).show();
}
}catch (SQLException e)
{
Toast.makeText(this,"please enter value",Toast.LENGTH_LONG).show();
}
}
public void myselect(View view) {
Intent i=new Intent(this,Select.class);
startActivity(i);
}
public void mydelete(View view) {
Intent i=new Intent(this,Delete.class);
startActivity(i);
}
public void myupdate(View view) {
Intent i=new Intent(this,Update.class);
startActivity(i);
}
}
Select.java
package com.example.pc131.databasedymaic;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class Select extends AppCompatActivity {
SQLiteDatabase mydb;
TextView tv;
String str=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
tv=findViewById(R.id.textView);
mydb=openOrCreateDatabase("mydb.db",0,null);
Cursor c=mydb.query("emp",new String[]{"id","fname","lname"},null,null,null,null,null,null);
StringBuffer str=new StringBuffer();
String result=null;
str.append("\t Display data\n"+"Result"+c.getCount()+"\n column"+c.getColumnCount());
String rheader=" || ";
for(int i=0;i<c.getColumnCount();i++)
{
rheader=rheader.concat(c.getColumnName(i).toLowerCase()+" || ");
}
str.append("\n\t\t"+rheader+"\n");
c.moveToFirst();
while(c.isAfterLast()==false)
{
str.append(result="\t");
for(int i=0;i<c.getColumnCount();i++)
{
result=result.concat(c.getString(i)+"\t");
}
str.append("\nRow"+c.getPosition()+":"+result);
c.moveToNext();
}
tv.setText(str);
}
public void myback(View view) {
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
}
package com.example.pc131.databasedymaic;
import android.content.Intent;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText e1 ,e2;
Button bi,bu,bd,bs;
SQLiteDatabase mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=findViewById(R.id.edit1);
e2=findViewById(R.id.edit2);
bi=findViewById(R.id.but1);
bu=findViewById(R.id.but2);
bd=findViewById(R.id.but3);
bs=findViewById(R.id.but4);
mydb=openOrCreateDatabase("mydb.db",0,null);
String createtbl="CREATE TABLE IF NOT EXISTS emp(id INTEGER PRIMARY KEY AUTOINCREMENT,FNAME TEXT,LNAME TEXT);";
mydb.execSQL(createtbl);
}
public void myinsert(View view) {
try
{
if(e1.getText().toString().trim().length()>0 && e2.getText().toString().trim().length()>0)
{
mydb.execSQL("insert into emp(fname,lname)values('"+e1.getText().toString()+"','"+e2.getText().toString()+"');");
Toast.makeText(this,"Records insert successfully",Toast.LENGTH_LONG).show();
e1.setText("");
e2.setText("");
}
else
{
Toast.makeText(this,"Enter the record",Toast.LENGTH_LONG).show();
}
}catch (SQLException e)
{
Toast.makeText(this,"please enter value",Toast.LENGTH_LONG).show();
}
}
public void myselect(View view) {
Intent i=new Intent(this,Select.class);
startActivity(i);
}
public void mydelete(View view) {
Intent i=new Intent(this,Delete.class);
startActivity(i);
}
public void myupdate(View view) {
Intent i=new Intent(this,Update.class);
startActivity(i);
}
}
Select.java
package com.example.pc131.databasedymaic;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class Select extends AppCompatActivity {
SQLiteDatabase mydb;
TextView tv;
String str=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
tv=findViewById(R.id.textView);
mydb=openOrCreateDatabase("mydb.db",0,null);
Cursor c=mydb.query("emp",new String[]{"id","fname","lname"},null,null,null,null,null,null);
StringBuffer str=new StringBuffer();
String result=null;
str.append("\t Display data\n"+"Result"+c.getCount()+"\n column"+c.getColumnCount());
String rheader=" || ";
for(int i=0;i<c.getColumnCount();i++)
{
rheader=rheader.concat(c.getColumnName(i).toLowerCase()+" || ");
}
str.append("\n\t\t"+rheader+"\n");
c.moveToFirst();
while(c.isAfterLast()==false)
{
str.append(result="\t");
for(int i=0;i<c.getColumnCount();i++)
{
result=result.concat(c.getString(i)+"\t");
}
str.append("\nRow"+c.getPosition()+":"+result);
c.moveToNext();
}
tv.setText(str);
}
public void myback(View view) {
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
}
Update.java
package com.example.pc131.databasedymaic;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Update extends AppCompatActivity {
EditText ei,ef,el;
SQLiteDatabase mydb;
Button bs,bu,bc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
ei=findViewById(R.id.edit1);
ef=findViewById(R.id.edit2);
el=findViewById(R.id.edit3);
bs=findViewById(R.id.but2);
bu=findViewById(R.id.but3);
bc=findViewById(R.id.but4);
mydb=openOrCreateDatabase("mydb.db",0,null);
bu.setEnabled(false);
ef.setEnabled(false);
el.setEnabled(false);
}
public void myfind(View view) {
try
{
if(ei.getText().toString().trim().length()>0)
{
Cursor c=mydb.query("emp",new String[]{"id","fname","lname"},"id='"+ei.getText().toString()+"'",null,null,null,null);
if(c.getCount()>0)
{
try
{
c.moveToNext();
ef.setEnabled(true);
el.setEnabled(true);
bu.setEnabled(true);
bs.setEnabled(false);
ei.setEnabled(false);
ef.setText(c.getString(1));
el.setText(c.getString(2));
}catch (Exception e)
{
e.printStackTrace();
}
}
else
{
Toast.makeText(this,"Record not found",Toast.LENGTH_LONG).show();
}
}
}catch (Exception e)
{
e.printStackTrace();
}
}
public void mycancle(View view) {
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}
public void myupdte2(View view) {
try
{
if(ef.getText().toString().trim().length()>0 && el.getText().toString().trim().length()>0)
{
mydb.execSQL("UPDATE emp set "+"fname='"+ef.getText().toString()+"'"+",lname='"+el.getText().toString()+"'"+"where id='"+ei.getText().toString()+"'");
Toast.makeText(this,"Records update successfully",Toast.LENGTH_LONG).show();
startActivity(new Intent(getApplicationContext(),MainActivity.class));
// ef.setText("");
// el.setText("");
}
}catch (SQLException e)
{
e.printStackTrace();
}
}
}
Delete.java
package com.example.pc131.databasedymaic;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Delete extends AppCompatActivity {
EditText e1;
SQLiteDatabase mydb;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete);
e1=findViewById(R.id.edit1);
b1=findViewById(R.id.but1);
mydb=openOrCreateDatabase("mydb.db",0,null);
}
public void mybtnclick(View view) {
try
{
if(e1.getText().toString().trim().length()>0)
{
Cursor c=mydb.query("emp",new String[]{"id"},"id='"+e1.getText().toString()+"'",null,null,null,null);
if(c.getCount()>0)
{
mydb.delete("emp","id=?",new String[]{e1.getText().toString()});
Toast.makeText(this,"Record delete",Toast.LENGTH_LONG).show();
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}
else
{
Toast.makeText(this,"Record not found",Toast.LENGTH_LONG).show();
}
}
}catch (Exception e)
{
e.printStackTrace();
}
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.pc131.databasedymaic.MainActivity">
<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter the First"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="6dp"
tools:layout_editor_absoluteY="0dp" />
<EditText
android:id="@+id/edit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter the Last"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="71dp" />
<Button
android:id="@+id/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert"
android:onClick="myinsert"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="165dp" />
<Button
android:id="@+id/but2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select"
android:onClick="myselect"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="165dp" />
<Button
android:id="@+id/but3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:onClick="mydelete"
tools:layout_editor_absoluteX="263dp"
tools:layout_editor_absoluteY="165dp" />
<Button
android:id="@+id/but4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:onClick="myupdate"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="267dp" />
</LinearLayout>
Select.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.pc131.databasedymaic.Select">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="151dp"
tools:layout_editor_absoluteY="35dp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myback"
android:text="Back" />
</LinearLayout>
Update.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.pc131.databasedymaic.Update">
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter id" />
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter First Name" />
<EditText
android:id="@+id/edit3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Last Name" />
<Button
android:id="@+id/but2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myfind"
android:text="Find" />
<Button
android:id="@+id/but3"
android:onClick="myupdte2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update" />
<Button
android:id="@+id/but4"
android:layout_width="match_parent"
android:onClick="mycancle"
android:layout_height="wrap_content"
android:text="Cancle" />
</LinearLayout>
Delete.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.pc131.databasedymaic.Delete">
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
/>
<Button
android:id="@+id/but1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="mybtnclick"
android:text="Delete" />
</LinearLayout>
GOOD WORK
ReplyDelete