Commit 763b968a authored by Adrien Oliva's avatar Adrien Oliva

feat(database): Create database scheme

Build sqlite database at activity creation.

GTLAB #1Signed-off-by: Adrien Oliva's avatarAdrien Oliva <olivaa+gitlab@yapbreak.fr>
parent 3bf5931b
Pipeline #460 passed with stage
in 0 seconds
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -16,15 +16,21 @@ import android.widget.ListView;
import java.util.ArrayList;
import fr.yapbreak.skyrimquest.data.DataHelper;
import fr.yapbreak.skyrimquest.quests.Adapter;
import fr.yapbreak.skyrimquest.quests.Quest;
public class QuestList extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private DataHelper quest_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
quest_data = new DataHelper(this);
quest_data.populate();
setContentView(R.layout.activity_quest_list);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
......
package fr.yapbreak.skyrimquest.data;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "quest_database";
private final static int DATABASE_VERSION = 1;
public final static String QUEST_TABLE = "tbl_quest";
public final static String CATEGORY_TABLE = "tbl_category";
public final static String CHARACTER_TABLE = "tbl_character";
public final static String ACHIEVEMENT_TABLE = "tbl_achievement";
public final static String PK_CATEGORY_ID = "pk_category_id";
public final static String RESOURCE_ID_STRING = "resource_id_string";
public final static String PK_QUEST_ID = "pk_quest_id";
public final static String FK_CATEGORY_ID = "fk_category_id";
public final static String PK_CHARACTER_ID = "pk_character_id";
public final static String NAME = "name";
public final static String PK_ACHIEVEMENT_ID = "pk_achievement_id";
public final static String FK_QUEST_ID = "fk_quest_id";
public final static String FK_CHARACTER_ID = "fk_character_id";
public final static String STATUS = "status";
public final static String CATEGORY_CREATION =
"CREATE TABLE " + CATEGORY_TABLE
+ "( " + PK_CATEGORY_ID + " INTEGER PRIMARY KEY"
+ ", " + RESOURCE_ID_STRING + " TEXT NOT NULL UNIQUE ON CONFLICT ROLLBACK"
+ ")";
public final static String QUEST_CREATION =
"CREATE TABLE " + QUEST_TABLE
+ "( " + PK_QUEST_ID + " INTEGER PRIMARY KEY"
+ ", " + RESOURCE_ID_STRING + " TEXT NOT NULL"
+ ", " + FK_CATEGORY_ID + " INTEGER REFERENCES " + CATEGORY_TABLE + "(" + PK_CATEGORY_ID + ") ON DELETE SET NULL"
+ ")";
public final static String CHARACTER_CREATION =
"CREATE TABLE " + CHARACTER_TABLE
+ "( " + PK_CHARACTER_ID + " INTEGER PRIMARY KEY"
+ ", " + NAME + " TEXT NOT NULL"
+ ")";
public final static String ACHIEVEMENT_CREATION =
"CREATE TABLE " + ACHIEVEMENT_TABLE
+ "( " + PK_ACHIEVEMENT_ID + " INTEGER PRIMARY KEY"
+ ", " + FK_QUEST_ID + " INTEGER REFERENCES " + QUEST_TABLE + "(" + PK_QUEST_ID + ") ON DELETE SET NULL"
+ ", " + FK_CHARACTER_ID + " INTEGER REFERENCES " + CHARACTER_TABLE + "(" + PK_CHARACTER_ID + ") ON DELETE SET NULL"
+ ", " + STATUS + " INTEGER"
+ ")";
public DataHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase)
{
sqLiteDatabase.execSQL(CATEGORY_CREATION);
sqLiteDatabase.execSQL(QUEST_CREATION);
sqLiteDatabase.execSQL(CHARACTER_CREATION);
sqLiteDatabase.execSQL(ACHIEVEMENT_CREATION);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
{
// TODO: When database scheme needs it!
}
public void populate()
{
insert_cat("cat_main_quest_act_1");
insert_cat("cat_main_quest_act_2");
insert_cat("cat_main_quest_act_3");
}
private void insert_cat(String resid)
{
SQLiteDatabase db = getWritableDatabase();
ContentValues cat = new ContentValues();
cat.put(RESOURCE_ID_STRING, resid);
db.insert(CATEGORY_TABLE, null, cat);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment