1 Kings 15:5 NIV
5 For David had done what was right in the eyes of the Lord and had not failed to keep any of the Lord’s commands all the days of his life—except in the case of Uriah the Hittite.
But David did make several mistakes throughout his lifetime
David carried out an unsanctioned census
1 Chronicles 21 NIV
21 Satan rose up against Israel and incited David to take a census of Israel. 2 So David said to Joab and the commanders of the troops, “Go and count the Israelites from Beersheba to Dan. Then report back to me so that I may know how many there are.”
David lied to Ahimelech
1 Samuel 21 NIV
2 David answered Ahimelek the priest, “The king sent me on a mission and said to me, ‘No one is to know anything about the mission I am sending you on.’ As for my men, I have told them to meet me at a certain place. 3 Now then, what do you have on hand? Give me five loaves of bread, or whatever you can find.”
David ate the consecrated bread
1 Samuel 21 NIV
5 David replied, “Indeed women have been kept from us, as usual whenever[b] I set out. The men’s bodies are holy even on missions that are not holy. How much more so today!” 6 So the priest gave him the consecrated bread, since there was no bread there except the bread of the Presence that had been removed from before the Lord and replaced by hot bread on the day it was taken away.
David was angry with God because of Uzzah
2 Samuel 6 NIV
8 Then David was angry because the Lord’s wrath had broken out against Uzzah, and to this day that place is called Perez Uzzah.[e]
So why is it said that David did not turn away from God's commands?