Good afternoon, I am simply trying to translate and know what the example codes say in order to better understand how each thing works, and I am stuck in this fregment, it is part of a code for a multi-table database in android studio, the code is the next...
(I get everything from this exercise http://instinctcoder.com/android-studio-sqlite-database-multiple-tables-example/ and my intention is to learn how to make a multi-table database in sqlite for android studio UU)
I want to know what it does or what it means and how to read the line, what is it getSimpleName();
?
public static final String TAG = Course.class.getSimpleName();
in this class
package com.instinctcoder.sqlitedbmultitbl.data.model;
/**
* Created by Tan on 1/26/2016.
*/
public class Course {
public static final String TAG = Course.class.getSimpleName();
public static final String TABLE = "Course";
// Labels Table Columns names
public static final String KEY_CourseId = "CourseId";
public static final String KEY_Name = "Name";
private String courseId;
private String name;
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and then... in this block I don't know the guys " data.repo
" who then use them like...
db.execSQL(CourseRepo.createTable());
db.execSQL(StudentRepo.createTable());
db.execSQL(MajorRepo.createTable());
db.execSQL(StudentCourseRepo.createTable());
in the class..
package com.instinctcoder.sqlitedbmultitbl.data;
/**
* Created by Tan on 1/26/2016.
*/
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.instinctcoder.sqlitedbmultitbl.app.App;
import com.instinctcoder.sqlitedbmultitbl.data.model.Course;
import com.instinctcoder.sqlitedbmultitbl.data.model.Major;
import com.instinctcoder.sqlitedbmultitbl.data.model.Student;
import com.instinctcoder.sqlitedbmultitbl.data.model.StudentCourse;
import com.instinctcoder.sqlitedbmultitbl.data.repo.CourseRepo;
import com.instinctcoder.sqlitedbmultitbl.data.repo.MajorRepo;
import com.instinctcoder.sqlitedbmultitbl.data.repo.StudentCourseRepo;
import com.instinctcoder.sqlitedbmultitbl.data.repo.StudentRepo;
public class DBHelper extends SQLiteOpenHelper {
//version number to upgrade database version
//each time if you Add, Edit table, you need to change the
//version number.
private static final int DATABASE_VERSION =8;
// Database Name
private static final String DATABASE_NAME = "sqliteDBMultiTbl.db";
private static final String TAG = DBHelper.class.getSimpleName().toString();
public DBHelper( ) {
super(App.getContext(), DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//All necessary tables you like to create will create here
db.execSQL(CourseRepo.createTable());
db.execSQL(StudentRepo.createTable());
db.execSQL(MajorRepo.createTable());
db.execSQL(StudentCourseRepo.createTable());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(TAG, String.format("SQLiteDatabase.onUpgrade(%d -> %d)", oldVersion, newVersion));
// Drop table if existed, all data will be gone!!!
db.execSQL("DROP TABLE IF EXISTS " + Course.TABLE);
db.execSQL("DROP TABLE IF EXISTS " + Student.TABLE);
db.execSQL("DROP TABLE IF EXISTS " + Major.TABLE);
db.execSQL("DROP TABLE IF EXISTS " + StudentCourse.TABLE);
onCreate(db);
}
}
The instruction
CualquierClase.class.getSimpleName()
returns the name of the class as aString
(in your case"Course"
), there is another called functiongetName
that gets the full name of the class, that is, the package name plus the name of the class (in your case"com.instinctcoder.sqlitedbmultitbl.data.model.Course"
). This has nothing to do with sqlite, it is part of the java standard.The variable
TAG
used in your code receives that value, since you later use it as a means of debugging with the following statement:This last expression will allow you to read the information you put in at program execution time. In your case you have the following code in the method
onUpgrade
that is executed when you update the database version:This will print the values of the old version and new version.
In this case it simply gets the name of the class to be used as a label when printing data inside the
LogCat
:In this case it is the same as if you simply wrote: