I am using SQLite database in my android studio project , I need to insert some default values to columns in a specific 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 ))
That's 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!
This is how I solve this problem:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Convert.db";
public static final String TABLE_DATUM="datum_table";
public static final String COL_ID="ID";
public static final String COL_NAME="NAME";
public static final String COL_A="A";
public static final String COL_B="B";
public static final String COL_SF="SCALEFACTOR";
public static final String COL_FEASTING="FEASTING";
public static final String TABLE_POINTS="point_table";
public static final String COL_PTID="PTID";
public static final String COL_PTNAME="PTNAME";
public static final String COL_NORTHING="NORTHING";
public static final String COL_EASTING="EASTING";
public static final String COL_LATITUDE="LATITUDE";
public static final String COL_LONGITUDE="LONGITUDE";
public static final String COL_CM="CM";
public static final String COL_ZONE="ZONE";
public static final String COL_DATUM="DATUMNAME";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " +TABLE_DATUM +"(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT, A REAL, B REAL, SCALEFACTOR REAL, FEASTING REAL)");
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_POINTS + "(PTID INTEGER PRIMARY KEY AUTOINCREMENT,PTNAME TEXT, NORTHING REAL, EASTING REAL,LATITUDE TEXT,LONGITUDE TEXT,CM TEXT,ZONE TEXT,DATUMNAME TEXT)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('WGS 84', 6378137, 6356752.314, 0.9996, 500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('NAD 83',6378137.00,6356752.314,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('GRS 80',6378137.00,6356752.314,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('WGS 72',6378135.00,6356750.5,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Australian 1965',6378160.00,6356774.7,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Krasovsky 1940',6378245.00,6356863,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('North American 1927',6378206.40,6356583.8,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('International 1924',6378388.00,6356911.9,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Hayford 1909',6378388.00,6356911.9,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Clarke 1880',6378249.10,6356514.9,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Clarke 1866',6378206.40,6356583.8,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Airy 1830',6377563.40,6356256.9,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Bessel 1841',6377397.20,6356079,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Everest 1830',6377276.30,6356075.4,0.9996,500000)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DATUM);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_POINTS);
onCreate(db);
}
///////////////////////////////////////////////////////////////////////////////////////
public void insertDatum (String name, double a, double b, double scalefactor, double falseeasting) {
SQLiteDatabase db =this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_NAME, name);
contentValues.put(COL_A, a);
contentValues.put(COL_B, b);
contentValues.put(COL_SF, scalefactor);
contentValues.put(COL_FEASTING, falseeasting);
db.insert(TABLE_DATUM, null, contentValues);
}
}
Might I suggest using the db.insert() method vs. using the execSQL method. Check out this documentation referencing the methods and their differences:
https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert(java.lang.String,%20java.lang.String,%20android.content.ContentValues)
For one it's not a void method, so you can check that the data was inserted successfully and do something if necessary, secondly it could simplify your code since all you need to feed it is the ContentValues and Tablename as String. See if that works better for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With