This is the first part of the SQLite for Android series that we are going to explain how to create simple data bases, add records, update records and delete data from it. Along the path of experiencing the data base handling in Android we will explain every detail regarding data handling in you Android App.
This first part of SQLite for Android, is just getting to know how to create a simple SQLite for Android database in your app.
ButterKnife library
First we add our favorite ButterKnife library in our build.gradle app file and sync the project:
1 2 3 4 |
dependencies { implementation 'com.jakewharton:butterknife:9.0.0-rc1' annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1' } |
DataBaseHelper class
In order to use SQLite database, first we will need to create it. To create database, we will need to create our own database Helper Class. In our example it is called DataBaseHelper and will extend SQLiteOpenHelper:
1 2 3 |
public class DataBaseHelper extends SQLiteOpenHelper { } |
Now we need to implement methods from SQLiteOpenHelper and implement default constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class DataBaseHelper extends SQLiteOpenHelper { public DataBaseHelper(@androidx.annotation.Nullable Context context, @androidx.annotation.Nullable String name, @androidx.annotation.Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } |
Database meta-data
We need a database name, name of table, columns and version. This fields are required so we can make a functional database. Every one are static final so they will be immutable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class DataBaseHelper extends SQLiteOpenHelper { // DB information public static final String DB_NAME = "Person.db"; // Camel case don`t apply & location of the file after creating is in data/data/app on device public static final String STUDENT_TABLE = "student_table"; // Table name public static final String COL_1_ID = "id"; // Column name public static final String COL_2_NAME = "name"; public static final String COL_3_LAST_NAME = "last_name"; public static final String COL_4_DATE_OF_BIRTH = "date_of_birth"; public static final String COL_5_GENDER = "gender"; public static final int VERSION = 1; . . . } |
Create the Database
For simplicity, we will modify our constructor so it will take only Context as parameter and give database name and version, Factory can be null. Also we will initialize the database here just for this example. Later it will be removed:
1 2 3 4 5 6 7 |
public DataBaseHelper(Context context) { super(context, DB_NAME, null, VERSION); // This below is just for checking on first run and see that the db is created // we will not need it in the later tutorials SQLiteDatabase sqLiteDatabase = this.getWritableDatabase(); } |
Creating table in database
In onCreate(SQLiteDataBase sqLiteDataBase) method we create the table of our previously created database. Inside onCreate there is argument of sqLiteDataBase database that we can use:
1 2 3 4 5 6 7 8 9 10 |
// execSQl() - Command that executes any quarry // Creating a table with given columns sqLiteDatabase.execSQL("CREATE TABLE " + STUDENT_TABLE + " (" + COL_1_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_2_NAME + " TEXT, " + COL_3_LAST_NAME + " TEXT, " + COL_4_DATE_OF_BIRTH + " TEXT," + COL_5_GENDER + " TEXT)" ); |
execSQL execute any query for the database, and in this we have CREATE for creating the table with name STUDENT_NAME, and append column 1 which is ID and is auto incremented, column 2 is NAME, column 3 is LAST_NAME, column 4 is DATE_OF_BIRTH and column 5 is GENDER. The ID is INTEGER and all others are TEXT fields. This is standard SQL syntax for creating table.
Upgrade database
If we need to upgrade the table we call onUpgrade() method. First we must check if our table exists, and if it does we are DROP -ping it (remove) and create new table calling onCreate. Again we are using execSQL method. In onUpdate, as a argument, we have our previously created database – sqLiteDatabase, that we have created in onCreate:
1 2 3 4 5 6 |
@Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { // In case of upgrading the table, first remove it completely and then create again with onCreate sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + STUDENT_TABLE); onCreate(sqLiteDatabase); } |
Initialize DataBaseHelper
In our MainActivity we declare DataBaseHelper with name dataBase. Initialization of this reference is in onCreate in MainActivity and we pass the context of the activity when we create the new DataBaseHelper object. DataBase is declared globally in MainActivity for future usage in the next parts of this tutorial:
1 2 3 4 5 6 7 8 9 10 11 |
public class MainActivity extends AppCompatActivity { DataBaseHelper dataBase; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dataBase = new DataBaseHelper(this); } } |
Now, when we start our app, in MainActivity in onCreate method, we create new dataBase file. To see that we actually did create a .db file we need to find it on our phone.
To do this, go to Android Studio –> View -> Tool Windows -> Device File Explorer OR in your bottom right corner of the Android Studio in vertical format. In here youll see your phones File Explorer. Select folder data –> data –> <yourapp>(example for this app is com.denofdevelopers.blogdbmanager_partone) –> databases –> Person.db. If there is this .db file, you have successfully created a database.
I’m really enjoying the design and layout of your blog. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Excellent work!|