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

    • 0
    • 6
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.10k
    Comment on it

    There are situation when we need to maintain(store) data at our application level, this is mainly done when we try to execute functionalists at offline mode. So for these kind scenarios android sdk has provide us class known as SQLiteOpenHelper with the help of which we can maintain a database.

    The very first step is to learn how to create a table. And before that you should know what is Table and Columns . It is very basic terminology of sqlite. So briefly we can say that table is a structure with various columns in which we store the data of a particular data type. Lets take up an example a very common example I must say :) . Suppose you need to save an employee data, here I'm just saving the name of employee, his salary, his designation. You can have more information as per your requirement.

    So first we need to declare the following before we move ahead :

    // 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 how to write a query for creating a table :-

    // Query for creating table for employee
            String employeeTableQuerry = "create table " + EMPLOYEE_TABLE + "( " + BaseColumns._ID
                    + " integer primary key autoincrement, " + EMPLOYEE_NAME + " text, "+ EMPLOYEE_DESIGNATION + " text, "+ EMPLOYEE_SALARY + " text not null);";
            Log.d("employeeData", "onCreate: " + employeeTableQuerry);
    

    Specifically the exact implementation in android you will find below :-

     private class DataBaseHelp extends SQLiteOpenHelper{
    
    // 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"; 
            @Override
            public void onOpen(SQLiteDatabase db) {
                super.onOpen(db);
            }
    
            @Override
            public void onCreate(SQLiteDatabase db) {
                // Query for creating table for employee
            String employeeTableQuerry = "create table " + EMPLOYEE_TABLE + "( " + BaseColumns._ID
                    + " integer primary key autoincrement, " + EMPLOYEE_NAME + " text, "+       EMPLOYEE_DESIGNATION + " text, "+ EMPLOYEE_SALARY + " text not null);";
            Log.d("employeeData", "onCreate: " + employeeTableQuerry);
    
                   db.execSQL(employeeTableQuerry);
            }
    
            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
            }
        }
    

    Hope this will help you in starting up with the sqlite database in android and creating a table inside, go through it and please feel free to ask if you have any query. :)

    Visit my next blog to learn How to Insert data in android SQLITE table

 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: