Sugar as a defence against bacteria?
Sugar in high concentrations acts osmotic. This means that the water available in the cells is drawn towards the high concentration of a solutant (sugar), like in the image below (this is demonstrated with a plant cell, but the principle is the same for bacteria and other microorganisms):



Since microorganisms can not survive without water, they are not able to grow or reproduce. This effectively safes food. The same mechanism works when salt is used to conserve meat or fish.