Trying to fix database issues.

Hi,

here is an attempt to fix #114. It's working fine for my needs but I wonder if I've correctly understood how to store collections into the database.