Database [ SQLite Database ] [ local database ]

Android SQLite Database

  1. SQLite is a local database, so it doesn't need any server or any online place to keep the date.
    1. that's why no need for any ODBC / JDBC  queries /connection for its functionality.
  2. It saves its data into a text file and it saves locally into your mobile or Android device.
  3. SQLite is an open source database.
  4. SQLite supports standard relational database features.
  5. Android comes in with built-in SQLite database implementation.
    1. so you don't need to install any extra libraries to use SQLite with Android.
  6. To create and upgrade a database in your Android application :
    1. you create a subclass of SQLiteOpenHelper class. 
    2. in the constructor of your subclass, you call the super() method of SQLiteOpenHelper.

Start Code:

  1. we are going to open an Android project.
  2. then we are going to create a java class [ ex: DatabaseHelper.java ] for handling the SQLite database.
    1. this class extends SQLiteOpenHelper.
    2. implements all the @Override methods. [ onCreate(), onUpgrade() ]
    3. implements Constructor matching super. 
      1. choose 1st of them, when creating it by ( ALT+ENTER ).
  3. code:
    public class DatabaseHelper extends SQLiteOpenHelper {
    
        public DatabaseHelper(Context context) {
            super(context, name, factory, version);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }
    code:
  4. Now for creating a database, we need a database name.

          1. Suppose our database looks like below:

          Student.db


  5. so, let's declare some global variables, which will help us:
    code:
    public static final String Database_Name = "student.db"; //SQLite not case sensitive
    public static final String Table_Name = "student_table";
    /*Now the columns : here we have 4 columns so*/
    public static final String Col_1 = "ID";
    public static final String Col_2 = "NAME";
    public static final String Col_3 = "SURNAME";
    public static final String Col_4 = "MARKS";  //
    code:
  6. Edit the constructor:
    code:
    public DatabaseHelper(Context context) {
    
        //super(context, name, factory, version);
        super(context, Database_Name, null, 1);
    }
    code:
    Note:
      whenever this constructor is called your database will be created locally into your Android device.

  7. Now we are going to create a Table in our database by onCreate() method and write some code for upgrade by onUpgrade() method:
    code:
    @Override
    public void onCreate(SQLiteDatabase db) {
    
     //Query is not case sensitive never 
    //String  query_for_createTable = "create table student_table (ID integer primary key autoincrement,NAME text,SURNAME text,MARKS integer)";
     //String  query_for_createTable = "create table "+Database_Name+" ("+Col_1+" integer primary key autoincrement,"+Col_2+" text,"+Col_3+" text,"+Col_4+" integer)";
    
        String  query_for_createTable = "create table "+Database_Name+" ("+
                Col_1+" integer primary key autoincrement,"+
                Col_2+" text,"+
                Col_3+" text,"+
                Col_4+" integer)";
        
        db.execSQL(query_for_createTable);
    
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
        //String query_for_dropTable = "drop table if exists student_table";
        String query_for_dropTable = "drop table if exists "+Database_Name+"";
        db.execSQL(query_for_dropTable);
        /*for upgrade table we need to drop the table 1st then create the table newly.*/
        onCreate(db);
    }
    code:
  8. Now In our mainAcivity.java, we are going to make an instance of  DatabaseHelper.java class.
    code:
    public class MainActivity extends AppCompatActivity {
    
        DatabaseHelper myDB;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            myDB = new DatabaseHelper(this);
        }
    }
    code:























মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি

API (Application Programming Interface) .

Add Gif file in Android xml file