SQLite Database Creation and Insertion
In this post will see about SQLite Database Creating and Insertion.
If we want to store huge data and we need to do some searching or shorting we need to use SQLite Database Android natively supports SQLite Database.
we don’t need to add any other Library to use this To demonstrate this, I am Creating a new application namely SqliteExample with default configuration .
In this Example, I am going to store name and age in SQLite Database Open “activity_main.xml” Change to root Layout to LinearLayout and add orientation as vertical remove the views inside the root layout Inside Creating one more LinearLayout with width as “match_parent” and height as “wrap_content”.
Adding an EditText for name with margin and padding 10dp assigning id as “etvName” duplicate the EditText by Selecting the code and Press ctrl+d on your keyboard adding orientation as vertical for the LinearLayout change the second EditText id as etvAge Age must be number.
So setting inputType as number Adding a Button, So that on click of this, we can insert the entered data into Database Setting margin and padding 10dp text as Insert and assigning id as “btn_insert” .
Creating new kotlin file to define the structure of User Object class User adding id, name and age attributes creating a constructor with name and age as parameters .
So that we can create a user object in single-line itself setting the parameters as attribute values to handle Database operations, we need a Handler class So Create a kotlin file .
We need some values like Database name,table name, column names etc. creating those variables. Create handler class as same as File Name with context as a parameter.
It extends SQLiteOpenHelper class inside we need to pass 4 parameters context, Database name, CursorFactory and Database version .
We are not going to use CursorFactory So passing null here. Giving Version as 1 we need to implement onCreate() and onUpgrade() from the base class.
This onCreate() will be executed, When the device doesn’t contain the Database So we need to create required tables here Here I am creating the table with id, name, and age as fields To execute query we need to use execSQL() .
Inside we need to pass the query string onUpgrade() will be executed, When we have a older version of Database.
You will get the oldVersion and currentVersion in this method itself using this we need to upgrade the table structure accordingly Creating a function insertData() to insert User object.
There are two types of SQLiteDatabase objects are there,
Here we are going to Write the data into Database .
So get the WritableDatabse To insert into Database we need ConventValues So, create ConventValues inside we need to put our values cv.put “COL_NAME” should store user.name cv.put “COL_AGE” should store user.age now we need to insert this ConventValues db.insert() inside we need to pass 3 parameters TABLE_NAME, null and ConventValues.
The second parameter is a String We need to pass any Column name here We don’t need any null column.
So we are passing null as Second Parameter this insert() will return rowID if the result is -1 then Some error is occurred so showing toast as “Failed” otherwise showing toast as “Success” Open “MainActivity.kt” setting the onclick listener for “btn_insert” if name and age-length is > 0 then we need to insert the data into Database.
Otherwise showing error toast Otherwise showing error toast by passing the entered value inside the constructor and storing it into a variable called “user” creating the helper class object using this calling insertData() which we created early Let’s run the application.
Entering the data and clicking on the “Insert” button we are getting toast as “Success” Which means the data is inserted successfully In next tutorial will see how to retrieve, update and delete the records from the database That’s all for this tutorial guys.