I have a this function below which is working fine:
I use it because in a synchronisation process I have to insert a lot of rows (sometimes more than 30 000 in the same sync) in different tables and for some of them, the value I recieve from my Json
object is not the real one. So before inserting the row, I have to replace the value with another. That is why I use this function:
public void myFunction( String tableName, String attrName, ContentValues currentVal ) {
SQLiteDatabase database = this.getReadableDatabase();
long realValue = -1;
Cursor c = database.rawQuery("SELECT value FROM "+tableName+" WHERE "+tableName+".other_value = " + currentVal.getAsString(attrName), null);
if( c.moveToFirst() ) {
realValue = c.getLong(0);
}
if( c != null ) c.close();
currentVal.put( attrName, realValue );
}
My problem is the Cursor
... The c.moveToFirst()
function
affects the speed of the process.
I did researches and I found so many explanations like :
I specify that I use indexes and I already try transactions.
So, what I wanted to do now (or to know) is: Is there a way to perform something like:
currentVal.put( attrName, SELECT value FROM "+tableName+" WHERE "+tableName+".other_value = " + currentVal.getAsString(attrName) );
Because with this trick I will not have to iterate into my Cursor
(which is actually my problem) and when I will insert the content of the ContentValues
it will automatically process the sub-query to retrieve the real value to insert.
I also thought about creating a temporary table. I insert the values founded in my Json
object without changing them and when I've inserted all the values for the current table, I copy them into the real table unsing in the same time a sub-query to relpace the incorrect values by the correct ones.
Any ideas, suggestions, snippets, ... will be appreciated