Android SQLite Insert Rows in Database

UPDATE: See Complete Application Example Here

Previous Part 1: Android SQLite: Integration.

Part 2: Insert Rows in SQLite Android App

In the previous post we worked on the integration of SQLite then we created Database and Table. Now we will Create a new Activity to insert a new row in Database.

So let’s start

Step 1) Add a button in activity_layout.xml to open a new Activity( InsertRowActivity)

 

Now in MainActivity.java define onClick method insertRowActivity

In this method, we are just opening InsertRowActivity.

 

Step 2) Create a new activity ( InsertRowActivity ). Right click on root package > New > Activity > Empty Activity 

InsertRowActivity.java will look like this. I will explain methods in a while.

NOTE: Mostly I use xdroid’s toast in projects you can find installation guide and advantages here

In InsertRowActivity we will have three EditText fields and a Button to get values from EditText and Insert a row in the database.

activity_insert_row.xml will have three EditText to take values from user and a button to save values, it will look like as below

In UsersDatabaseAdapter.java which we created in the previous post, we will add a new method (insertEntry) to Insert row data in the database and getRowCount to return total rows in the table.

Find Source code here

After this our app will be able to insert user data in the database.

 

Leave a Reply

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