RoyceNguyen / BuildCalculator

0 stars 0 forks source link

Database #8

Open Blazeblackbird opened 7 years ago

Blazeblackbird commented 7 years ago

@cfiliault hey Cai, I emailed you but forgot I can create an issue here.

so I've decided to create predefined items into our Database, then for the listview, create a spinner and have the user choose one of the existing items then upload that build. So the user will select a weapon, a piece of gear (or more not sure yet, might have to create a few slots like gloves, armor, boots) then name the build themselves and upload.

The problem I am running into is for the Database, how do I create my own items that will show up in the spinner for the respected slot? is it something like;

db.execSQL("INSERT INTO " + TABLE_WEAPON + "(NAME, ATTACKDAMAGE, CRIT, CRITDAMAGE, ATTACKSPEED ) VALUES ('RAGE', 5000, 0.5, 400, 1.5)");

Is the line above how I create my own items into a table?

cfiliault commented 7 years ago

@Blazeblackbird Yes you would use a statement similar to the one you have listed above.

Inside the onCreate() of your database you will want to add the statements of all the default data you wish to add similar to the following:


public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_BUILDS_TABLE);

        db.execSQL(CREATE_PICTURES_TABLE);

        db.execSQL(CREATE_IMAGE_LOCATION_TABLE);

        db.execSQL("INSERT INTO " + TABLE_WEAPON + "(NAME, ATTACKDAMAGE, CRIT, CRITDAMAGE, ATTACKSPEED ) VALUES ('RAGE', 5000, 0.5, 400, 1.5)");

}

This will ensure the items are only ever added one time (when the database is first created). Please note: You will need to uninstall and reinstall your app every time you wish to make change to those statements.

Blazeblackbird commented 7 years ago

@cfiliault Okay, I have all the items created in TABLE_WEAPON and TABLE_GEAR in the onCreate using the db.execSQL as I was doing. then I have;

public void addGear(Item item) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_NAME, item.getName()); values.put(COLUMN_ARMOR, item.getArmor()); values.put(COLUMN_HEALTH, item.getHealth()); values.put(COLUMN_MAGICRESIST, item.getMagicResist()); db.insert(TABLE_GEAR, null, values); db.close(); }

public void addWeapon(Item item) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COLUMN_NAME, item.getName());
    values.put(COLUMN_ATTACKDAMAGE, item.getAttackDamage());
    values.put(COLUMN_ATTACKSPEED, item.getAttackSpeed());
    values.put(COLUMN_CRIT, item.getCrit());
    values.put(COLUMN_CRITDAMAGE, item.getCritDamage());
    values.put(COLUMN_MAGICDAMAGE, item.getMagicDamage());
    db.insert(TABLE_WEAPON, null, values);
    db.close();
}

In the create for CRUD. Now I'm not sure what to do for the Read, Update and Delete. Also, for creating the items, when I'm using a decimal do I need to make it a double instead of an int in my Item class? for example, attackspeed is 1.6