So I have this piece of code in Java(Android) to add a list of brokers to a local SQLite database as one single sql instruction.
public void Add(List<Broker> brokers)
{
if(brokers == null || brokers.size() == 0)
return;
String sql = "INSERT INTO " + TABLE_NAME + " SELECT " + brokers.get(0).getId() + " AS '" + COLUMN_BROKERID + "', "+ brokers.get(0).getOfficeId() + " AS '" + COLUMN_OFFICEID + "', '"+ brokers.get(0).getName() + "' AS '" + COLUMN_NAME + "', "+ brokers.get(0).getSuccessRate() + " AS '" + COLUMN_SUCCESSRATE + "'";
for(int i=1; i<brokers.size(); i++)
{
sql = sql + " UNION SELECT " + brokers.get(i).getId() + ", " + brokers.get(i).getOfficeId() + ", '" + brokers.get(i).getName() + "', " + brokers.get(i).getSuccessRate();
}
databaseManager.ExecuteNonQuery(sql);
}
But what slows this down a lot is the change in the string 'sql'. The last line, which is a call to ExecuteNonQuery() takes a millisecond, but the above take a lot. How can I speed this up?
databaseManager is an Interface that is implemented by a class of SQLiteOpenHelper and simply executes an sql statement(but don't take this into account, it doesnt take any time as much as the rest of the code)