The sugary foods advice is true if you're talking about refined sugars, but I recall a study showing that raisins, e.g., are much better than refined flour in terms of damage to teeth. Despite having much higher sugar content.
Here's an article about the study: https://nutritionfacts.org/blog/raisins-and-dental-health/