In toddlers, pinching is quite common and begins as a pleasurable experience for the youngster. It simply feels good to get mommy's skin rolling around between ever-strengthening fingers. However, as time goes by it becomes quite annoying and develops into a bad habit.
The old adage of 'pinch them back' is just not acceptable these days as it promotes, rather than dissuades, the behaviour. Better to talk the infant out of it and hold them at bay while doing so.
In six to twelve year olds, the activity is less common but still occurs, and especially when least appropriate. The less hurtful forms can occur when you are engaged in a conversation and the child wants to be heard. This isn't the 'craving attention' notion but more the 'listen to me' urgency. So it wasn't due to some absence of affection necessarily, just a need to be heard at that moment.
Of course it all takes planning. As parents, you can't expect your children to behave well in public if you haven't taught them before hand. It is vital to describe, and even do role play, to explain the dynamics of a conversation. A conversation involving more than two or three people.
It is a real joy when you run across a polite youngster that is able to interject because they know how. Teach them how to deal with the frustration of not getting your attention immediately. Understand that they may want to communicate with you but they obviously are not doing it well.
If your child understands the ground rules of dealing with you when you are occupied or resting, then they will feel more comfortable that they can get your attention when needed because they will know how to get it without annoying you.
It's a win-win situation. The old school approach was 'children should be seen and not heard', and although still appreciated, it is less than practical.
Train them first, rather than battle them forever.