patternjavaMinor
Database data insertion
Viewed 0 times
databaseinsertiondata
Problem
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
Solution
For exact knowledge about finding out which is the most effective, the best way to find that out is to time using:
I think your way of doing it currently is quite good.
I found that there is another way of doing it but it seems to involve writing a seemingly SQL query that looks like this:
Unless that alternative way of inserting rows to the database improves performance significantly, I would stick to the way that you are doing it now. I imagine that the code required to write this SQL statement would be quite ugly (if you would want to transform your current
Also, your current approach is very easy to read and understand.
As for whether or not you should write a
Summary
Stick to what you are using right now.
long start = System.nanoTime();
... perform operations ...
long stop = System.nanoTime();
double milliSecondsElapsed = (stop - start) / 1000000.0;I think your way of doing it currently is quite good.
I found that there is another way of doing it but it seems to involve writing a seemingly SQL query that looks like this:
INSERT INTO Contacts
SELECT 'ae0caa6a-8ff6-d63f-0253-110b20ac2127' AS ID, 'xxx' AS FirstName, 'xxx' AS LastName, '9008987887' AS PhoneNumber, 'xxx@gmail.com' AS EmailId, 'Yes' AS Status
UNION SELECT '9afab56e-a18a-47f2-fd62-35c78d8e0d94', 'yyy', 'yyy', '7890988909', 'yyy@gmail.com', 'Yes'
UNION SELECT '378d757a-ee60-07a4-e8bc-396b402c3270', 'zzz', 'zzz', '9000898454', 'zzz@gmail.com', 'Yes'Unless that alternative way of inserting rows to the database improves performance significantly, I would stick to the way that you are doing it now. I imagine that the code required to write this SQL statement would be quite ugly (if you would want to transform your current
String[]/ContentValues approach into this SQL statement). Especially considering that I assume you only perform this mass-insertion once. Besides, the code required to transform into SQL also takes time to execute of course, which might neglect the performance increase you would get from doing the mass-insertion with a single SQL statement.Also, your current approach is very easy to read and understand.
As for whether or not you should write a
bulkInsert() method, you could do it just for the challenge of it... if you don't have anything better to do :)Summary
Stick to what you are using right now.
Code Snippets
long start = System.nanoTime();
... perform operations ...
long stop = System.nanoTime();
double milliSecondsElapsed = (stop - start) / 1000000.0;INSERT INTO Contacts
SELECT 'ae0caa6a-8ff6-d63f-0253-110b20ac2127' AS ID, 'xxx' AS FirstName, 'xxx' AS LastName, '9008987887' AS PhoneNumber, 'xxx@gmail.com' AS EmailId, 'Yes' AS Status
UNION SELECT '9afab56e-a18a-47f2-fd62-35c78d8e0d94', 'yyy', 'yyy', '7890988909', 'yyy@gmail.com', 'Yes'
UNION SELECT '378d757a-ee60-07a4-e8bc-396b402c3270', 'zzz', 'zzz', '9000898454', 'zzz@gmail.com', 'Yes'Context
StackExchange Code Review Q#48312, answer score: 5
Revisions (0)
No revisions yet.