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

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


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.


So I added this code, and it worked :

@Overridepublic void onCreate(SQLiteDatabase db) {    db.execSQL(CREATE_TABLE);    db.execSQL(CREATE_TABLE2);    ContentValues contentValues=new ContentValues();    contentValues.put(DBhelper.MySettings_TaarifHolDay,1.0);    contentValues.put(DBhelper.MySetting_TarrifFriday,1.5);    contentValues.put(DBhelper.MySetting_TarrifSaturday,1.5);    db.insert(DBhelper.Table_NAME2,null,contentValues);}