mercredi 16 décembre 2015

Android Bulk Insert Using Hashmap

I'm having problems trying to bulk insert all information comes from a JSON to the SQLite database.

Below, I'm showing the construction of the data that I need to insert in one time.

if (unsync.length() != 0) {
    for (int i = 0; i < unsync.length(); i++) {
        JSONObject needSync = (JSONObject) unsync.get(i);

        // DB QueryValues unsync to insert into SQLite
        queryUnsync = new HashMap<String, String>();
        queryUnsync.put("Code", needSync.get("Code").toString());
        queryUnsync.put("Name", needSync.get("Name").toString());
    }

    dbAdapter mDb;
    mDb = new dbAdapter(getContext());
    mDb.open();
        mDb.bulkInsert(queryUnsync);
    mDb.close();
}

Here, is the function to control the bulk insert. On it, I pass the hashmap to get the itens to insert.

public void bulkInsert (HashMap<String, String> queryValues) throws SQLException {
    String sql = "INSERT OR REPLACE INTO " + DATABASE_TABLE + " VALUES (?, ?);";
    SQLiteStatement statement = mDb.compileStatement(sql);
    mDb.beginTransaction();

    statement.bindLong(1, Integer.valueOf(queryValues.get("Code")));
    statement.bindString(2, queryValues.get("Name"));

    mDb.setTransactionSuccessful();
    mDb.endTransaction();
}

However, it is not adding any register to the database. Could someone help me? I need to insert all at the same time.

Thank you

Aucun commentaire:

Enregistrer un commentaire