jeudi 14 janvier 2016

Query in ContentValues

I have to insert a lot of rows in my SQLite Database and for some tables a specific value of each row has to be converted into an other using the value of an other table. Actually, I have this function which is working well:

public void myFunction( String tableName, String attrName, ContentValues currentVal ) { 

    SQLiteDatabase database = this.getReadableDatabase();
    Cursor c = database.rawQuery("SELECT colName FROM "+tableName+" WHERE "+tableName+".otherColName = " + currentVal.getAsString(attrName), null);

    Long realValue = null;
    if( c.moveToFirst() ) { 
        realValue = c.getLong(0);
    }
    if( c != null ) c.close();

    currentVal.put( attrName, realValue );

}

But, this is really time consumimg so I wanted to know if there's a way to set the query directly in the value of the ContantValue like this:

currentVal.put( attrName, "SELECT colName FROM "+tableName+" WHERE "+tableName+".otherColName = " + currentVal.getAsString(attrName));

and the query will be executed at the same time as the insert query in order to replace the value.

Thank you beforehand!

Aucun commentaire:

Enregistrer un commentaire