Tuesday, 17 September 2019

Android App using SQLite Database

               In this post, we will see How to Create Android App using SQLite Database. 

                How to create Android First App Hello World using Android Studio, check in following link:
                To know how to install Android Studio in Linux, check following link:

                To find details about Architecture of Android Operating System, check following link:


Program (StudentDbHelper.java) 

package com.example.sqlitedbapp;

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

public class StudentDbHelper extends SQLiteOpenHelper {

    public StudentDbHelper(Context context) {

    public void onCreate(SQLiteDatabase db){
        db.execSQL("create table student (sid varchar(10),sname varchar(10),marks varchar(10))");

    public void onUpgrade(SQLiteDatabase db, int oldversion,int newversion){
        db.execSQL("drop table if exists student");


Program (MainActivity.java)

package com.example.sqlitedbapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        StudentDbHelper dbHelper = new StudentDbHelper(this);

        //To insert values into database
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("sid", "1");
        values.put("sname", "Parag");
        values.put("marks", "80");

        long row = db.insert("student", null, values);
        System.out.println("row number is" + row);

        //To retrieve values from database

        SQLiteDatabase db1 = dbHelper.getReadableDatabase();
        String projection[] = {"sid", "sname", "marks"};
        Cursor c = db.query("student", projection, null, null, null, null, null);
        System.out.println("Name is " + c.getString(1));


No comments:

Post a comment