This is one place where the NIV translation falls short. The quoted passage says “[David did not fail] to keep any of the LORD’s commands.” However the Hebrew word used here is soor, which means to turn aside. Note that the NIV cross-references Deut. 5:32 and 1Ki 9:4, which both talk about turning away from God’s commands. This is one passage where other translations give a better picture:
because David did what was right in the sight of the Lord, and had not turned aside from anything that He commanded him all the days of his life, except in the case of Uriah the Hittite. (1Ki 15:5, NASB)
because David did what was right in the eyes of the Lord and did not turn aside from anything that he commanded him all the days of his life, except in the matter of Uriah the Hittite. (1Ki 15:5, ESV)
So, where the NIV’s talks about keeping the commands, these other translations talk more about a lifestyle. David knew of God’s mercies that bring forgiveness for his sins (Ps 25:6-7). The sacrificial system of the OT provided a means for sinners to come into communion with God (though not as perfectly as the sacrifice of Christ). From this passage we see that David’s lifestyle, similar to many Christians today, was one where he would sometimes mess up, but he always saw his mistake right away and he came right back to God.
The exception of Uriah was a sin that David did not repent of right away. First, we know he hid it until at least after Bathsheba announced her pregnancy. Apparently he had relations right after her period (2Sam 11:4) and if we assume she knew about the pregnancy when her next period was due, that was about one month in between. Then add the time to kill Uriah, and we have easily have weeks to months of him living in this sin before he repents (2Sam 12:13, Ps 51). So, while he did eventually repent, this was one point where David was clearly walking outside of God’s laws, vice a quick fall and get back up.