The text in question was written during the reign of a very sinful king of Judah, Abijah, who reigned briefly in Jerusalem. The key point about the assessment of this king is in verse 3 which tells us:
"And he walked in all the sins of his father, which he had done before
him: and his heart was not perfect with the Lord his God, as the heart
of David his forefather." 1 Kings 15:3 A.V.
The key point is this comparison between the heart condition of those two kings. This is what is being assessed. A person whose heart is "right" before God will still commit sins, because nobody is sinless - nobody is perfect - but one whose heart beats for God, and not for self, will repent of their sin and accept God's disciplining of them without complaint, knowing it is justified.
On the other hand, a wicked person may go through the motions of appearing to keep God's laws, but when they sin, they have no regard for the enormity of sin in God's eyes. They will not repent, and when God deals with them, they will complain bitterly. (See Revelation 9:20-21 & 16:21.) That is why king David is brought into the equation now. Verse 4 continues,
"Nevertheless for David's sake did the Lord his God give him a lamp in
Jerusalem, to set up his son after him, and to establish Jerusalem:
because David did that which was right in the eyes of the Lord, and
turned not aside from any thing that he commanded him all the days of
his life, save only in the matter of Uriah the Hittite." 1 Kings
Notice that David's sin with Uriah's wife is not glossed over? It is specifically mentioned as the blot on David's copy-book. Everyone is being told that David did what was wrong by turning aside from the Lord's commands on that dreadful matter. Yet God had promised David a line of descent om the throne of Jerusalem, and God would keep his promise despite David's awful sin. Remember too that David repented, and accepted the Lord's discipline on him and his family?
So, there is no suggestion that David was exempt from some of God's commands. His sin in that respect is flagged up once more. But because his heart was right before God, that is the crucial difference between David's sins and Abijah's sins, and the outcome for Abijah shows that God had not forgiven him, whereas he had forgiven David, when David repented.