Cavities aren't caused only by indulging in sweets and neglecting to brush your teeth, though these things can certainly contribute to getting them.
The real cause of cavities, though, is bacteria in your mouth that produce enamel-eroding acids when they feast on sugary and starchy foods.
Factors like dry mouth (which in turn can be caused by alcohol consumption and mouth breathing), frequent snacking, and snoring can all increase your risk of getting cavities.