SQLiteDatabase Program
Activity_main.xml code:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please enter the below details"
android:textSize="20dp"
android:gravity="center"
android:layout_marginTop="20dp"
android:id="@+id/info"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_below="@+id/info"
android:hint="Name"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/contact"
android:layout_below="@+id/name"
android:hint="Contact"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dob"
android:layout_below="@+id/contact"
android:hint="Date of birth"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dob"
android:layout_marginTop="50dp"
android:text="Insert"
android:id="@+id/insert"
/>
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/insert"
android:layout_alignBottom="@+id/insert"
android:layout_marginLeft="10dp"
android:text="Update"
/>
<Button
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/update"
android:layout_alignBottom="@+id/update"
android:layout_marginLeft="10dp"
android:text="View"
/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/view"
android:layout_alignBottom="@+id/view"
android:layout_marginLeft="10dp"
android:text="Delete"
/>
</RelativeLayout>
[Link] code:-
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText name,contact,dob;
Button insert,delete,view,update;
Database db;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
name = (EditText) findViewById([Link]);
contact = (EditText) findViewById([Link]);
dob = (EditText) findViewById([Link]);
insert = (Button) findViewById([Link]);
update = (Button) findViewById([Link]);
view = (Button) findViewById([Link]);
delete = (Button) findViewById([Link]);
db = new Database(this);
[Link](new [Link]() {
@Override
public void onClick(View view) {
String user = [Link]().toString();
String con = [Link]().toString();
String date = [Link]().toString();
Boolean check = [Link](user,con,date);
if(check == true){
[Link]([Link], "Data inserted successfully",
Toast.LENGTH_SHORT).show();
}
else{
[Link]([Link], "Data failed to insert",
Toast.LENGTH_SHORT).show();
}
}
});
[Link](new [Link]() {
@Override
public void onClick(View view) {
String user = [Link]().toString();
String con = [Link]().toString();
String date = [Link]().toString();
Boolean check = [Link](user,con,date);
if(check == true){
[Link]([Link], "Data updated successfully",
Toast.LENGTH_SHORT).show();
}
else{
[Link]([Link], "Data failed to update",
Toast.LENGTH_SHORT).show();
}
}
});
[Link](new [Link]() {
@Override
public void onClick(View view) {
String user = [Link]().toString();
Boolean check = [Link](user);
if(check == true){
[Link]([Link], "Data deleted successfully",
Toast.LENGTH_SHORT).show();
}
else{
[Link]([Link], "Data failed to delete",
Toast.LENGTH_SHORT).show();
}
}
});
[Link](new [Link]() {
@Override
public void onClick(View view) {
Cursor res = [Link]();
if([Link]() == 0){
[Link]([Link], "No data", Toast.LENGTH_SHORT).show();
return;
}
StringBuffer buffer = new StringBuffer();
while([Link]()){
[Link]("Name: "+[Link](0)+"\n");
[Link]("Contact: "+[Link](1)+"\n");
[Link]("Date of Birth: "+[Link](2)+"\n\n");
}
[Link] builder = new [Link]([Link]);
[Link](true);
[Link]("Student Information");
[Link]([Link]());
[Link]();
}
});
}
}
[Link] code:-
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Database extends SQLiteOpenHelper {
public Database(Context context) {
super(context, "[Link]", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
[Link]("create Table studentInfo(name TEXT primary key,contact TEXT,dob TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
[Link]("drop Table if exists studentInfo");
}
public Boolean insertData(String name,String contact,String dob){
SQLiteDatabase db = [Link]();
ContentValues contentValues = new ContentValues();
[Link]("name",name);
[Link]("contact",contact);
[Link]("dob",dob);
long result = [Link]("studentInfo",null,contentValues);
if (result == -1){
return false;
}
else {
return true;
}
}
public Boolean updateData(String name,String contact,String dob) {
SQLiteDatabase db = [Link]();
ContentValues contentValues = new ContentValues();
[Link]("contact", contact);
[Link]("dob", dob);
Cursor crs = [Link]("Select * from studentInfo where name=?", new String[]{name});
if ([Link]() > 0) {
long result = [Link]("studentInfo", contentValues, "name=?", new String[]{name});
if (result == -1) {
return false;
} else {
return true;
}
} else {
return false;
}
}
public Boolean deleteData(String name) {
SQLiteDatabase db = [Link]();
Cursor crs = [Link]("Select * from studentInfo where name=?", new String[]{name});
if ([Link]() > 0) {
long result = [Link]("studentInfo", "name=?", new String[]{name});
if (result == -1) {
return false;
} else {
return true;
}
} else {
return false;
}
}
public Cursor getData(){
SQLiteDatabase db = [Link]();
Cursor crs = [Link]("Select * from studentInfo",null);
return crs;
}
}
Output:-