0% found this document useful (0 votes)
7 views8 pages

Sqlite Program

The document contains an Android application that utilizes SQLite for managing student records. It includes an XML layout for the user interface with input fields for roll number, name, and branch, along with buttons for adding, updating, deleting, and displaying records. The MainActivity.java and DBHelper.java files implement the functionality to interact with the SQLite database, allowing CRUD operations on student data.

Uploaded by

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

Sqlite Program

The document contains an Android application that utilizes SQLite for managing student records. It includes an XML layout for the user interface with input fields for roll number, name, and branch, along with buttons for adding, updating, deleting, and displaying records. The MainActivity.java and DBHelper.java files implement the functionality to interact with the SQLite database, allowing CRUD operations on student data.

Uploaded by

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

Sqlite program:

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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sqlite_database"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:hint="Enter roll no:"/>

<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:hint="@string/name"/>

<EditText
android:id="@+id/et3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:hint="Branch:" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#F44336"
android:padding="20dp"
android:text="add"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="100dp"
/>

<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#8A0707"
android:padding="20dp"
android:text="update"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="60dp"
/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal"
>

<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#F44336"
android:padding="20dp"
android:text="delete"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="100dp"
/>

<Button
android:id="@+id/bt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#630A0A"
android:padding="20dp"
android:text="display"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="60dp"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv1"
android:textSize="20dp"
android:hint="records:"
android:text=""/>

</LinearLayout>
MainActivity.java
package com.example.databaseexample;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


EditText et1, et2, et3;
TextView tv1;
DBHelper dbHelper;
Button b1, b2, b3, b4;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et1 = findViewById(R.id.et1);
et2 = findViewById(R.id.et2);
et3 = findViewById(R.id.et3);
b1 = findViewById(R.id.bt1);
b2 = findViewById(R.id.bt2);
b3 = findViewById(R.id.bt3);
b4 = findViewById(R.id.bt4);
tv1 = findViewById(R.id.tv1);

dbHelper = new DBHelper(MainActivity.this);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String t1 = et1.getText().toString();
String t2 = et2.getText().toString();
String t3 = et3.getText().toString();
dbHelper.insert(t1, t2, t3);
et1.setText("");
et2.setText("");
et3.setText("");
}
});
// Update data
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String t1 = et1.getText().toString();
String t2 = et2.getText().toString();
String t3 = et3.getText().toString();
dbHelper.insert(t1, t2, t3);

}
});
//Delete Data
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String m_rollnumber = et1.getText().toString();

dbHelper.deletedata(m_rollnumber);

}
});

b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display_data();
}
});
}

private void display_data()


{
Cursor cur = dbHelper.getData();
StringBuilder sb = new StringBuilder();
while (cur.moveToNext()) {
String t1 = cur.getString(0);
String t2 = cur.getString(1);
String t3 = cur.getString(2);
sb.append("\n Roll no="+t1);
sb.append("\n Name="+t2);
sb.append("\n Branch="+t3 +"\n");

}
tv1.setText(sb.toString());
}
}
DBHelper.java
package com.example.databaseexample;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;

public class DBHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME="student.db";
private static final String TABLE_NAME="Student_table";
private static final String COL1="ROLL_NUMBER";
private static final String COL2="FIRST_NAME";
private static final String COL3="BRANCH";

public DBHelper(@Nullable Context context)


{

super(context,DATABASE_NAME, null, 1);


}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL( "CREATE TABLE "+TABLE_NAME + "(ROLL_NUMBER text PRIMARY KEY,
FIRST_NAME text, BRANCH text)");

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS Student_table");
onCreate(db); // Corrected variable name
}
public boolean insert(String rollnumber, String firstname, String branch)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL1, rollnumber);
cv.put(COL2, firstname);
cv.put(COL3, branch);
long insert_value=db.insert(TABLE_NAME, null, cv);
return insert_value!=-1;
}
public boolean update(String rollnumber, String firstname, String branch)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL1,rollnumber);
cv.put(COL2, firstname);
cv.put(COL3, branch);
int result_update_id=db.update(TABLE_NAME,cv,COL1+"=?",new String[]
{rollnumber});
return result_update_id>0;
}
public boolean deletedata(String rollnumber)
{
SQLiteDatabase db = this.getWritableDatabase();
int delete_result_id=db.delete(TABLE_NAME,COL1+"=?",new String[]{rollnumber});
return delete_result_id>0;
}

public Cursor getData()


{
SQLiteDatabase db = this.getReadableDatabase();
return db.rawQuery("SELECT * FROM "+TABLE_NAME, null);

}
}

You might also like