sqlite upsert and minor fixes