mardi 11 août 2015

quick insert sqlLite android

Sorry for my english, i learn sqlLite in android. And i try create universal method who insert to data base. Its slow insert:

 public long addVlues(String tableName, ContentValues cv) {
        long result = db.insert(tableName, null, cv);
        return result;
    }

But its method universal, i can use it when i want. Example:

ContentValues cv = new ContentValues();
cv.put("name", "test");
dao.addVlues("daoTest", cv); //this i calling method addValues

Its good and simple code(for me), but its slow.

Bellow fast insert code:

public void addValueTest(String tableName) {
        String sql = "insert into "+ tableName + " (id, name) values (?, ?);";

        db.beginTransaction();
        SQLiteStatement stmt = db.compileStatement(sql);
        for (int i = 0; i < 1000; i++) {
            stmt.bindString(i, "name");

            stmt.clearBindings();
            Log.e("qqq", String.valueOf(i));
        }

        db.setTransactionSuccessful();
        db.endTransaction();
    }

But for each table i need create single method, it increases code. I have question. How create universal fast insert method?

Aucun commentaire:

Enregistrer un commentaire