public class DataBaseHandler extends SQLiteOpenHelper {
private static String DATABASE_NAME = "UnitDatabase";
private static String MEASUREMENT_TYPE_TABLE = "measurement_types";
private static String idCol = "id";
private static String typeCol = "type";
private static int DATABASE_VERSION = 1;
private static String[] measurementType = new String[] {"acceleration", "angles", "area", "astronomical",
"density", "energy", "force", "frequency", "length/distance", "power", "pressure", "speed",
"temperature", "torque", "volume", "weight"};
public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String create_table = "CREATE TABLE " + MEASUREMENT_TYPE_TABLE +
"(" + idCol + " integer primary key autoincrement " +
typeCol + " varchar(255) not null ";
db.execSQL(create_table);
}
/**
* Populate the table containing measurement types
* @param db
*/
public void populateMeasurementTable(SQLiteDatabase db) {
db = this.getWritableDatabase();
for(int i = 0; i < measurementType.length; i++) {
ContentValues values = new ContentValues();
values.put(typeCol, measurementType[i]);
db.insert(MEASUREMENT_TYPE_TABLE, null, values);
}
}
}
I thought I would post my code first and then ask the question. My question is with my implementation of the last method (populateMeasurementTable()
). I wanted to be able to insert multiple values in to the table and this is the way I am going to do it, however I don't think it is an efficient way of doing it especially if I have larger arrays such as:
private static String[] densityUnitTypes = new String[] { "grain/cubic foot", "grain/cubic inch",
"grain/gallon [UK]", "grain/gallon [US]", "grain/ounce [UK]", "grain/ounce [US]", "grain/quart [UK]",
"grain/quart [US]", "gram/cubic centimeter", "gram/cubic kilometer", "gram/cubic meter",
"gram/cubic millimeter", "gram/kiloliter", "gram/liter", "gram/litre", "gram/microliter", "gram/milliliter",
"hectogram/cubic centimeter", "hectogram/cubic kilometer", "hectogram/cubic meter",
"hectogram/cubic micrometer", "hectogram/cubic millimeter", "hectogram/hectoliter", "hectogram/kiloliter",
"hectogram/liter", "hectogram/litre", "hectogram/microliter", "hectogram/milliliter",
"kilogram/cubic centimeter", "kilogram/cubic kilometer", "kilogram/cubic meter",
"kilogram/cubic micrometer", "kilogram/cubic millimeter", "kilogram/kiloliter", "kilogram/liter",
"kilogram/litre", "kilogram/microliter", "kilogram/milliliter", "kiloton/cubic mile [UK]",
"kiloton/cubic mile [US]", "kiloton/cubic yard [UK]", "kiloton/cubic yard [US]", "kilotonne/cubic meter",
"kilotonne/kiloliter", "kilotonne/liter", "kilotonne/litre", "microgram/cubic centimeter",
"microgram/cubic kilometer", "microgram/cubic meter", "microgram/cubic micrometer",
"microgram/cubic millimeter", "microgram/cubic nanometer", "microgram/kiloliter", "microgram/liter",
"microgram/litre", "microgram/microliter", "microgram/milliliter", "milligram/cubic centimeter",
"milligram/cubic kilometer", "milligram/cubic meter", "milligram/cubic millimeter",
"milligram/kiloliter", "milligram/liter", "milligram/litre", "milligram/microliter", "milligram/milliliter",
"nanogram/cubic centimeter", "nanogram/cubic kilometer", "nanogram/cubic meter",
"nanogram/cubic millimeter", "nanogram/kiloliter", "nanogram/liter", "nanogram/litre",
"nanogram/microliter", "nanogram/milliliter", "ounce/cubic foot", "ounce/cubic inch", "ounce/gallon [UK]",
"ounce/gallon [US]", "pound/cubic foot", "pound/cubic inch", "pound/cubic mile", "pound/cubic yard",
"pound/gallon [UK]", "pound/gallon [US]", "tonne/cubic kilometer", "tonne/cubic meter", "tonne/kiloliter",
"tonne/liter", "tonne/litre", "water [0°C, solid]", "water [20°C]", "water [4°C]" };
I had thought about using the bulkInsert(Uri uri, ContentValues[] values)
method from the ContentResolver
class in the API however, I felt that writing own provider class to extend the ContentProvider
class and then writing my own implementation of the bulkInsert()
method was a little nuclear for this - although it would allow me to use transactions which I have read a far more efficient way to carry out the task.
So my questions are:
- How efficient is the way I have chosen to do it?
- Would I be better of writing my own implementation of the
bulkInsert()
method? - Is there a different more efficient way? (for example writing the raw SQL out to insert multiple values)