Hi again, here you will learn how to insert data in already created table in SQLITE. You will find it helpful if you are well aware with basic terminology SQLITE if not please go through my previous blog. It will might help you to understand this better.
So lets take up the same example of Employee table, now we have to insert the data in table Employee containing the columns :- name, designation and salary, as I have mentioned before you can have more columns as per you requirement, it is just an illustration. We will use the same declaration as we have done before:-
// Table name
private static String EMPLOYEE_TABLE = "Employee_Table";
// Columns names
private static String EMPLOYEE_NAME = "Employee_Name";
private static String EMPLOYEE_DESIGNATION = "Employee_Designations";
private static String EMPLOYEE_SALARY = "Employee_Salary";
To make it modular and simple it is better to have a function which will collectively perform our motive :-
/**
* Inserting all the Employee data in DB
* @param employee name
* @param employee designation
* @param employee salary
*/
public void insertEmployeeData(String name , String designation, String salary){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(EMPLOYEE_NAME, name);
values.put(EMPLOYEE_DESIGNATION, designation);
values.put(EMPLOYEE_SALARY, salary);
db.insert(EMPLOYEE_TABLE, null, values);
db.close();
}
This method should be contained in the your database class which is extended by SQLiteOpenHelper
so "getWritableDatabase" helps to makes the sqlite editable enable it to insert information. In this way you can insert data, hope you will find it useful. In the next session we will learn you to retrieve the data from table.
Enjoy and have a nice day ... !!
0 Comment(s)