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

[sociallocker] Download SQLiteMultiTableDEmonuts [/sociallocker]

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...

25 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:

    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?


  9. Kev says:

    Thanks, it finally clicked into place. Great example…

  10. Bert says:

    hello i followed your tutorial it is very informative but i would like to know how to make the recording from two activity
    example activity 1 to user activity 2 for hobby I want to know how we handle the id in these cases thank you

    • Hardik Parsania says:

      userModelArrayList is the easiest way to handle users across all the classes of whole project.
      We are getting userModelArrayList in main activity. If you want to use userModelArrayList in other class just make it public.

  11. Angela says:

    Hi, I followed your tutorial, I tweaked a bit to go along with my assignments for a student database. It stores the data, but when I try to view all users I receive this error:
    java.lang.IllegalStateException: Couldn’t read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

    How can I resolve this error?

    • Hardik Parsania says:

      There is a error in your DatabaseHelper class.

    • Ricardo Souto says:

      Maybe? Suppose are in de first record and you make a previus move , give -1 number.
      You need to check all first/last /next / previos calls .
      In Koltrin i need to genereta a First when jump the limits.

  12. Tom Higbie says:

    This tutorial is very helpful. I do have a problem – when I click on the button to show all users, the app crashes. Any ideas? Thanks for your help.

  13. Tom says:

    Get a 403 error when I tried to post logcat. SOLVED! In the past when I’ve seen this problem, one of the xml files was incorrect. In this case, it was the manifest. The getAllUsersActivity was not listed in the manifest. Thanks for your response.

  14. Tom says:

    I get a 403 error when I post the logcat. I solved the problem. GetAllUsersActivity was not in the manifest.Works great! Thanks for a good example

  15. Ellay says:

    Thanks it works! But can you help me create a mobile application that computes data from two tables?

Leave a Reply

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