Attempt to re-open an already-closed object: java.lang.IllegalStateException:?

后端 未结 3 1075
野性不改
野性不改 2021-01-04 06:29

I know this question has asked many times in SO,but i couldn\'t figure out my exact problem.

I am using the following code to get the data from the database(Table1)

相关标签:
3条回答
  • 2021-01-04 07:12

    What if you comment out db.updateAverage(id, average) ?

    0 讨论(0)
  • 2021-01-04 07:16

    You can't update a table while iterating over the results of a query. There are good reasons for this; what if the data you are adding would cause a change to the data you're iterating over? Your cursor would not return valid data.

    Trying to store data back into the table in updateAverage() is causing the problem. You should just remember the average value during your loop, and then update it once at the end after you've finished looping over your cursor.

    To further explain the exact error you're getting: the act of inserting new data is causing the database to close all the cursors which are currently open, as a safety measure. So when you call sum_cursor.moveToNext() after updating the average, the cursor is a bit surprised to find that it's already been closed.

    0 讨论(0)
  • 2021-01-04 07:16

    You can achieve your goal with pure SQL it is faster and better from architecture point of View because all logic will be in one SQL transaction

    REPLACE INTO table_where_to_put  SELECT *, (totalmatchedscore/totalingredients) as average  FROM table_with_input_data
    

    Use REPLACE OR UPDATE

    0 讨论(0)
提交回复
热议问题