Android Studio: Fetch Data From Local Storage /SQLite(2020) in your app— PART 1 (Creating Database)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">

<EditText
android:id="@+id/editTextID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:ems="10"
android:hint="Enter product ID"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textViewName"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.486" />

<TextView
android:id="@+id/textViewID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:layout_marginLeft="56dp"
android:text="Product ID:"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.366" />

<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:text="Product Name :"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.484" />

<EditText
android:id="@+id/editTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginEnd="28dp"
android:layout_marginRight="28dp"
android:ems="10"
android:hint="Enter Product Name"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/textViewID"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.365" />

<Button
android:id="@+id/buttonSave"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="232dp"
android:background="@drawable/button_selector"
android:elevation="10dp"
android:text="Save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.479"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextID" />

</androidx.constraintlayout.widget.ConstraintLayout>
//name of the table and columns
public static final String TABLE_NAME = "products";
public static final String COLUMN_ID = "productID";
public static final String COLUMN_NAME = "productName";

//setting database name and version

public static final String DB_FILE_NAME = "products.db";
public static final int DB_VERSION = 1; // for database version

//sql statement to call create method
public static final String SQL_CREATE = "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME + " TEXT" + ");";
//public static final String SQL_DELETE = "DROP TABLE " + TABLE_NAME;

public DatabaseHelper(Context context) {

super(context, DB_FILE_NAME,null,DB_VERSION);
SQLiteDatabase db = this.getWritableDatabase();
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL(SQL_DELETE);
onCreate(db);


}
package com.nirvik.localstorageapp.database;

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


public class DatabaseHelper extends SQLiteOpenHelper {


SQLiteDatabase db;

//naming column of table

public static final String TABLE_NAME = "products";
public static final String COLUMN_ID = "productID";
public static final String COLUMN_NAME = "productName";

//setting database name and version

public static final String DB_FILE_NAME = "products.db";
public static final int DB_VERSION = 1; // for database version

//sql statement to call create method
public static final String SQL_CREATE = "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME + " TEXT" + ");";
public static final String SQL_DELETE = "DROP TABLE " + TABLE_NAME;


//constructor for dbHelper
public DatabaseHelper(Context context) {

super(context, DB_FILE_NAME, null, DB_VERSION);
SQLiteDatabase db = this.getWritableDatabase();
}


//passing the create sql command
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE);
}


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

db.execSQL(SQL_DELETE);
onCreate(db);


}
}
package com.nirvik.localstorageapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.nirvik.localstorageapp.database.DatabaseHelper;

public class MainActivity extends AppCompatActivity {



DatabaseHelper myDB; //Creating instance

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



myDB = new DatabaseHelper(this); //invoking the constructer







}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nirvik Basnet

Nirvik Basnet

Software Engineer• AI/ML• Mobile & Web Developer