fix database query bugs