Both sentences are correct, but it depends on what you are trying to say. The second sentence is used very often and is used to explain that you used to live somewhere in the past. The sentence can be used on its own to make a statement of fact. The first sentence is used when describing an event in the past and something would be expected to follow this statement. For example, 'I had been living in that small town for three years when I was a student. Then, things started to change...' I cannot explain the grammar as I am not a tutor, but I believe the words 'I lived' are 'past perfect', a completed action in the past. The words 'I had been living' refer to an ongoing action in the past that occurs when someone is telling a story or describing an event. I do not know the name of this tense, but maybe a tutor will answer this.