Android SQLite Database Tutorial (Select, Insert, Update, Delete)




SQLite is an open-source social database i.e. used to perform database operations on android gadgets, for example, putting away, controlling or recovering relentless information from the database.

It is implanted in android bydefault. In this way, there is no compelling reason to play out any database setup or organization assignment.
Here, we are going to see the case of sqlite to store and get the information. Information is shown in the logcat. For showing information on the spinner or listview, move to the following page. SQLiteOpenHelper class gives the usefulness to utilize the SQLite database.

A Simple Android SQLite Example
So lets create a project.

Step 1  – Create new Android project.

Provide Activity name as SQLiteApp as shown below

Android SQLite Database Tutorial
Android SQLite Database Tutorial . Create Project

 

Step 2 – Add components in the main activity as shown in the picture below.

 

Android SQLite Database Tutorial. Design Activity
Android SQLite Database Tutorial. Design Activity

\AndroidStudioProjects\SqliteApp\app\src\main\res\layout\activity_main.xml

 

Step 3 – Now create a new Java class called DatabaseHelper.java.

Android SQLite Database Tutorial. DatabaseHelper
Android SQLite Database Tutorial. DatabaseHelper

Add the following code in this class to DatabaseHelper.java

MainActivity.java

OUTPUT

Android SQLite Database Tutorial. Add Data
Android SQLite Database Tutorial. Add Data
Android SQLite Database Tutorial. Data Successfully added to sqlite database
Android SQLite Database Tutorial. Data Successfully added to sqlite database

 

Android SQLite Database Tutorial. View Data
Android SQLite Database Tutorial. View Data

 

Video Instructions


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com


  • utsav

    it is showing 2 errors…
    1.cannot find symbol variable menu
    2.cannot find symbol variable action_settings

    • SlovakianProgrammer

      Same error .

    • Abdallahi

      I have commented those 2 methods including menu variable and action_Setting and it gose well 🙂

  • adnan

    very helpful videos and code.. thank you buddy 🙂

  • thank you for the video + the code.

  • Fajar Rahmadi

    Nice tutorial, but where code for menu_main.xml ?

    • bunga

      i need the answer for this question also

      • Mrinalini Chava

        me too

    • Imam Abdurasyid

      same question

    • Vytenis Gavelis

      Just comment out the last two blocks ( onCreateOptionsMenu(Menu menu) and onOptionsItemSelected(MenuItem item) ) and the program will work. It doesn’t seem that “menu” has been realized at all, so these two are unnecessary.

    • Rahul Soshte

      Create a menu folder in res
      Then create menu_main.xml file in it and paste this code in it.

      • Rahul Soshte

        the Options Menu is not used but it can be helpful for different purposes

  • maksood

    THIS APP IS CLOSING AFTER CLICKING ON ADD BUTTON, WHY it only says
    “Unfortunately app has stopped” please help me

    • pao david

      Mee Too

  • Arnold Brady

    Well done!
    Just delete code for code for menu_main.xml

  • 3125130032 ??? ??????? ??? ???

    thanx for code

  • Emma Nexus

    how can i display that data into the table

  • Abigail Leary

    gives me this error when implemented into my code.
    (int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
    Please help?

  • Vytenis Gavelis

    Thank You very much for this example. Exactly what I needed to jump-start with SQLite.

  • Iftikhar Iqbal

    HI THERE
    I HAVE USED YOUR CODE FOR UNDERSTANDING PURPOSE BUT AFTER ADDING THE CODE IN MAIN ACTIVITY I GET THIS @DEMIN ERROR AND SECONDLY AFTER CLICK ON ADD THE APPLICATION STOPS,,

    PLZ GUIDE
    THANKS
    IFTIKHAR

  • ehis

    please how can i connect to mysql database using android
    my email ehisboy100@gmail.com

  • Asia Razdwatrzy

    SQLiteDatabase db = this.getWritableDatabase();
    this line in getAllData crashes whole application. could anyonne tell my why?

  • darshan parekh

    i want to add search bar in this please help

  • maisarah Adam

    here the id is primary key right? How to..
    1)save the data based on dateTime “now” (I don’t want to use datePickerTime or any calendar)just refer on date time device
    2)Retrieve data based on date selected by used calendar.