I can't tell what you've been doing, but probably what you need to do is something like
1. Do all your work in the memory db, without
touching the flash db.
2. Then, just before you detach, update the flash
db with
insert [or replace] into flash.db ...
select ... from memory.db ...
Regards

