Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Inserting data in android SQLITE table

    • 0
    • 7
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 929
    Comment on it

    Hi again,
    Here we are going to learn how to insert data in the already created table in android SQLITE. You will find it useful if you are aware of the basic terminology of the android SQLITE, if not and you want to create a table so it will be better you look my previous blog, Creating a table in android SQLITE.

    For inserting the data you should be having the table and the columns in which you have to insert data, I am taking the same example, I had table employee and it's respective columns i.e. name , designation and salary. I suppose, I don't have to mentions is just an illustration so you can have as many columns you want. Using the same declaration :-

    // 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"; 
    

    Now it is a better to write a single function which can carry out our motive, so in order to achieve modulation I am here writing a method for inserting the employee data :-

            /**
         * Inserting all the Employee data in DB
         * @param emplyoyeeName
         * @param employeeDesignation
             * @param employeeSalary
         */
        public void insertEmployee(String emplyoyeeName , String employeeDesignation, String employeeSalary){
            SQLiteDatabase db = this.getWritableDatabase();
    
            ContentValues values = new ContentValues();
            values.put(EMPLOYEE_NAME, emplyoyeeName);
            values.put(EMPLOYEE_DESIGNATION, employeeDesignation);
                    values.put(EMPLOYEE_SALARY, employeeSalary);
    
            db.insert(EMPLOYEE_TABLE, null, values);
    
            db.close();
        }
    

    Place this method inside your database class which will be extended by SQLiteOpenHelper, as here "this" will provide you the object of the database class and "getWritableDatabase()" enables it to be edited. So in this manner you will be able to insert the data in the employee table. Do have a good look, next we will learn how to retrieve the data, till then enjoy .

    Have a nice day . .!

 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: