Navigate to the app > java > your app’s package name > Right-click on it
> New > Java class and name it as DBHandler and add the below code
import [Link];
import [Link];
import [Link];
import [Link];
public class DBHandler extends SQLiteOpenHelper {
private static final String DB_NAME = "coursedb";
private static final int DB_VERSION = 1;
private static final String TABLE_NAME = "mycourses";
private static final String ID_COL = "id";
private static final String NAME_COL = "name";
private static final String DURATION_COL = "duration";
private static final String DESCRIPTION_COL = "description";
private static final String TRACKS_COL = "tracks";
public DBHandler(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// below method is for creating a database by running a sqlite query
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME + " ("
+ ID_COL + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ NAME_COL + " TEXT,"
+ DURATION_COL + " TEXT,"
+ DESCRIPTION_COL + " TEXT,"
+ TRACKS_COL + " TEXT)";
[Link](query);
// this method is use to add new course to our sqlite database.
public void addNewCourse(String courseName, String courseDuration, String
courseDescription, String courseTracks) {
SQLiteDatabase db = [Link]();
ContentValues values = new ContentValues();
[Link](NAME_COL, courseName);
[Link](DURATION_COL, courseDuration);
[Link](DESCRIPTION_COL, courseDescription);
[Link](TRACKS_COL, courseTracks);
[Link](TABLE_NAME, null, values);
[Link]();
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// this method is called to check if the table exists already.
[Link]("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
// creating variables for our edittext, button and dbhandler
private EditText courseNameEdt, courseTracksEdt, courseDurationEdt,
courseDescriptionEdt;
private Button addCourseBtn;
private DBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// initializing all our variables.
courseNameEdt = findViewById([Link]);
courseTracksEdt = findViewById([Link]);
courseDurationEdt = findViewById([Link]);
courseDescriptionEdt = findViewById([Link]);
addCourseBtn = findViewById([Link]);
// creating a new dbhandler class
// and passing our context to it.
dbHandler = new DBHandler([Link]);
// below line is to add on click listener for our add course button.
[Link](new [Link]() {
@Override
public void onClick(View v) {
// below line is to get data from all edit text fields.
String courseName = [Link]().toString();
String courseTracks = [Link]().toString();
String courseDuration = [Link]().toString();
String courseDescription = [Link]().toString();
// validating if the text fields are empty or not.
if ([Link]() && [Link]() &&
[Link]() && [Link]()) {
[Link]([Link], "Please enter all the
data..", Toast.LENGTH_SHORT).show();
return;
// on below line we are calling a method to add new
// course to sqlite data and pass all our values to it.
[Link](courseName, courseDuration,
courseDescription, courseTracks);
// after adding the data we are displaying a toast message.
[Link]([Link], "Course has been added.",
Toast.LENGTH_SHORT).show();
[Link]("");
[Link]("");
[Link]("");
[Link]("");
});
<EditText
android:id="@+id/idEdtCourseName"
<EditText
android:id="@+id/idEdtCourseDuration"
<EditText
android:id="@+id/idEdtCourseTracks"
<EditText
android:id="@+id/idEdtCourseDescription"
<Button
android:id="@+id/idBtnAddCourse"