samedi 31 octobre 2015

How to insert values into database only when created ?(SQLite Android Studio)

Im using SQLite database in my android studio project , I need to insert some default values to columns in a spesific table in my database only when it created for the first time ! ("for the first time" -I mean only when the database(and the table I want set the default values) is created! not when it opens(but not created ))

Thats my dbHelper Class :

public class DBhelper extends SQLiteOpenHelper {
//Table Name
public static final String TABLE_NAME = "MyShifts";
public static final String Table_NAME2="MySettings";
//Table coloumns
public static final String _ID = "_id";
public static final String MyShifts_EnteyHour = "enteryhour";
public static final String MyShifts_ExitHour = "exithour";
public static final String MyShifts_EnteryDate = "enterydate";
public static final String MyShifts_ExitDate = "exitdate";
public static final String MyShifts_Taarif="Tarrif";
public static final String MyShifts_Bonus="Bonus";
public static final String MyShifts_Comments="Comments";
public static final String MyShifts_Month="month";
public static final String MyShifts_year="year";
public static final String MyShifts_Duration="duration";
public static final String MyShifts_WeekDay="weekday";

public static final String MySettings_ID="id";
public static final String MySettings_TaarifHolDay="TaarifHol";
public static final String MySetting_TarrifFriday="TaarifFriday";
public static final String MySetting_TarrifSaturday="TaarifSatudrday";
//DataBase Information
static final String DB_NAME="Mydb.db";
// database version
static final int DB_VERSION = 1;

// Creating table query
private static final String CREATE_TABLE = "create table " + TABLE_NAME + "                 (" + _ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + MyShifts_EnteyHour
        + " TEXT NOT NULL, " + MyShifts_ExitHour + " TEXT NOT NULL, " + MyShifts_EnteryDate + " TEXT NOT NULL, "
        + MyShifts_ExitDate + " TEXT NOT NULL, "
        + MyShifts_Month + " INTEGER NOT NULL, " + MyShifts_year + " INTEGER NOT NULL, " + MyShifts_Taarif + " REAL NOT NULL, " + MyShifts_Bonus +

 " INTEGER NOT NULL, " + MyShifts_Comments +" TEXT, " + MyShifts_Duration + " TEXT NOT NULL, " + MyShifts_WeekDay+ " TEXT NOT NULL);";

private static final String CREATE_TABLE2= "create table " + Table_NAME2 + "("+ MySettings_ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
         MySettings_TaarifHolDay + " REAL NOT NULL, " + MySetting_TarrifFriday + " REAL NOT NULL," + MySetting_TarrifSaturday + "REAL NOT NULL);";

public DBhelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE);
}
public void OnCreate(SQLiteDatabase db){db.execSQL(CREATE_TABLE2);}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    db.execSQL("DROP TABLE IF EXISTS " + Table_NAME2);
    onCreate(db);
}

}

which and where code should I add in order to add default values to "MySettings" table when its created for the first time(created and not open) ? Thank you!

Aucun commentaire:

Enregistrer un commentaire