Reputation: 434
I created a game in which the user learns words in English and their meaning. every word have its translation, knowledge level, how meny times the word has been played and other.. all this data is saved in a db. the this is that I want to give the user option to delete all the data of the game (only the number of time shoed, knowledge lever and other stuf, not the words and the translation).
I tried going over all the rows in the db and setting values (0 for the most part). the problem is that there is about 6000 words, and it takes about 20 seconds to go over all the words.
is there a quicker way, can I ma?
what I did is :
public void resetData() {
Cursor c = ourDatabase.query(DATABASE_TABLE, null, null, null, null, null, null);
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_TURNS, 0);
cvUpdate.put(KEY_KNOWN, 1);
cvUpdate.put(KEY_KNOWLEDGE, 0);
cvUpdate.put(KEY_SHOWN, 0);
cvUpdate.put(KEY_CURRECTANSWER, 0);
cvUpdate.put(KEY_WRONGANSWER, 0);
int i = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
i++;
StaticData.mw.wordList.get(i).resetWord();
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "=" + i, null);
StaticData.dialog.setProgress(i);
}
}
Upvotes: 0
Views: 52
Reputation: 180080
If you don't use explicit transactions, each SQL command gets its own automatic transaction, and it's the transaction overhead that dominates the running time.
When executing many commands, you should wrap them into a single transaction:
db.beginTransaction();
try {
for (...)
...
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
However, if you are updating all rows with the same values, you can just do this with a single command:
ourDatabase.update(DATABASE_TABLE, cvUpdate, null, null);
Upvotes: 1