Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Inserting data in SQLITE table android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3
    Comment on it

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

    SQLITE android insert data in table Android database android

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: