0% found this document useful (0 votes)
32 views

Sqlite

The document contains code for an Android application that uses a SQLite database to store student records. It includes the layout file for the main activity, which contains edit texts and buttons to add, delete, and view student records. It also includes the DbHelper class to manage interactions with the SQLite database, including methods to insert, delete, and retrieve records. The MainActivity class wire up onclick listeners for the buttons to call the corresponding DbHelper methods and display the records.

Uploaded by

CM-Hitesh Tolani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Sqlite

The document contains code for an Android application that uses a SQLite database to store student records. It includes the layout file for the main activity, which contains edit texts and buttons to add, delete, and view student records. It also includes the DbHelper class to manage interactions with the SQLite database, including methods to insert, delete, and retrieve records. The MainActivity class wire up onclick listeners for the buttons to call the corresponding DbHelper methods and display the records.

Uploaded by

CM-Hitesh Tolani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

AndroidManifest.

xml

No changes

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"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint = "Enter name of the student"/>
<Button
android:id="@+id/add"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="insert record"
/>
<Button
android:id = "@+id/delete"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text = "DELETE RECORD"/>
<Button
android:id = "@+id/view"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="VIEW ALL RECORDS"/>

</LinearLayout>

DbHelper.java
package com.example.student;

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

public class DbHelpher extends SQLiteOpenHelper {


SQLiteDatabase db;
public DbHelpher(Context c)
{
super(c,"STUDENT_DATA",null,1);
db = this.getWritableDatabase();
String[] names = {"Akshay","Aditya","Shirish","Atharva","Tejas"};
if(!isTableExists()) {
for (int i = 0; i < names.length; i++) {
insert(names[i]);
}
}
}

@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS STUDENT(ID INTEGER PRIMARY KEY
AUTOINCREMENT,NAME TEXT)";
db.execSQL(sql);

}
public void insert(String name)
{
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("NAME",name);
db.insert("STUDENT",null,values);

}
public void delete(String name)
{
db = this.getWritableDatabase();
db.delete("STUDENT","NAME = ?",new String[]{name});

}
public Cursor getAllRecords()
{
db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM STUDENT",null);
return c;
}
public boolean isTableExists()
{
db = this.getReadableDatabase();
boolean tableExists = false;
try {
Cursor c = db.rawQuery("SELECT * FROM STUDENT", null);
c.close();
tableExists = true;
}
catch (SQLiteException e)
{

}
return tableExists;

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

MainActivity.java
package com.example.student;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


Button add,delete,view;
EditText name;
DbHelpher helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = findViewById(R.id.add);
delete = findViewById(R.id.delete);
view = findViewById(R.id.view);
name = findViewById(R.id.name);
helper = new DbHelpher(getApplicationContext());
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str_name = name.getText().toString();
helper.insert(str_name);
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str_name = name.getText().toString();
helper.delete(str_name);

}
});
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor c = helper.getAllRecords();
Log.d("CURSOR_LEN", String.valueOf(c.getCount()));
while(c.moveToNext())
{
String msg = "ID is "+c.getString(0)+" NAME is
"+c.getString(1);
Toast.makeText(MainActivity.this, msg,
Toast.LENGTH_SHORT).show();
}
c.close();
}
});
}
}

You might also like