SQLite With Multiple Tables In Android Studio Example

Hello, geeks. Welcome to SQLite with multiple tables in Android Studio example.

SQLite with multiple tables in Android example guides you to create multiple tables with simple source code.

We will implement crud operations like insert, update, delete and display data with multiple tables.

If you have not basic idea about SQLite, then check SQLite In Android Example first.

If you want to do all crud operations with one table, then visit Crud Operations In SQLite in Android.

You will need to develop SQLite with multiple tables in Android for complex projects.

First, check the output of SQLite with multiple tables in Android example, then we will develop it.

Download Source Code For SQLite With Multiple Tables In Android

Creating SQLite With Multiple Tables In Android Studio Tutorial step by step

Understanding Table Structure Of SQLite With Multiple Tables In Android

sqlite with multiple tables in android

Table Structure

We have created three tables.

  1. users – User’s name is stored in this table.
  2. users_hobby – User’s hobby is stored here.
  3. users_city – User’s city is stored here.

Now a column “id” is the key thing. We will reference all the values across the tables by “id.”

In users table, id for peter is 1. In users_hobby table, a hobby corresponds to id 1 is “spider man.” Thus, a hobby for peter is spider man.

In the users_city table, a city corresponds to id 1 is “new york.” Thus, a city for peter is new york. 

Similarly, you can see in above image that hobby for bruce is batman and city is gotham.

Now come to Android Studio and follow steps to create SQLite with multiple tables in Android.

Step 1: Create a new project in Android Studio.

Step 2: Creating UserModel class

Make a Java class named “UserModel” and add following source code

Step 3: Making DatabaseHelper class

Prepare a class named “DatabaseHelper” and all following source code

Step 4: Description of DatabaseHelper

Database name, Database version, table name and column names of tables are written as below.

Understanding most important part of SQLite Database

In onCreate() method, create statement for the table is written.

In onUpgrade() method, already exist tables are dropped and then all the tables are recreated.

onCreate() method is called into onUpgrade() method to create tables.

In following scenarios, you need to upgrade DATABASE_VERSION number in increment order.

  1. When you add, update or delete any column of any table in the whole database.
  2. When you update any column name of any table.
  3. When you add, update or delete any table.
  4. When you update table name.

When DATABASE_VERSION is updated in increment order, onUpgrade() method is called.

addUser() method will add the user to SQLite Database.

getAllUsers() method will fetch all the users from SQLite Database.

updateUser() method will update the user’s information.

deleteUser() method will delete the user from SQLite.

Step 5: Preparing lv_item.xml file

Create a layout resource file named “lv_item.xml” and add the following

Step 6: Preparing CustomAdapter class

Create a Java class named “CustomAdapter” and paste below

Step 7: Getting All Users From SQLite Database

Make a new activity named “GetAllUsersActivity.

Add below source code in GetAllUsersActivity.java

Paste below code in activity_get_all_users.xml

Step 8: Updating and deleting user information

Create a new activity named “UpdateDeleteActivity

Add below source code in UpdateDeleteActivity.java

Copy following code in activity_update_delete.xml

Step 9: Preparing MainActivity

Add below code in MainActivity.java

Copy following in activity_main.xml

So that is all for SQLite with multiple tables in Android example.

Feel free to comment your queries and reviews in the comment section. Thank you.

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

12 Responses

  1. ali says:

    Hey, Hardik appreciatable work done for beginners. The tutorial is simple and step by step. Can you provide the source code.

    • Hardik Parsania says:

      I have already shared download button in every tutorial at the beginning. Just like demonut’s facebook page and download button will be unlocked.

  2. Kusnadi says:

    hy, why the id’s name is equal with another table?
    can I make the id with another name?

  3. Sh Ani says:

    hi! I need to create two tables in sqlite and want to save a word in one table and other table contain its meaning and when ever i write the word in text field from one table it will fetch its meaning from other table and show me the required result how can i do that?? Please Help

  4. akash chauhan says:

    how to add more edit texts

  5. Chantal says:

    Hello Hardrik,

    thanks for your instructions.

    It would be helpful if you could add short comments on the Java classes, what they do, what they serve.

  6. reshma says:

    hi
    Code is working to add values to database but its not working to fetch records…

  7. Sunil Bhagwat says:

    This is very good example But I want to add multiple tables for eg. age, Plan, term, ppt, prem etc and I want one to many relationship between one to another table and retrive data to textview can you pl. give any example for that

  8. AMIN W says:

    Hello buddy, can u provide this project by Kotlin Language?

    thanks

Leave a Reply

Your email address will not be published. Required fields are marked *