David was not claiming to be sinless (note how in v. 11 he asks God for redemption and mercy). He was emphasizing his faith in God and how he exercised that. When we have faith it is imputed to us as righteousness.
May our faith be displayed today in all we say and do.
1 Vindicate me, O Lord, for I have led a blameless life; I have trusted in the Lord without wavering.
2 Test me, O Lord, and try me, examine my heart and my mind;
3 for your love is ever before me, and I walk continually in your truth.
4 I do not sit with deceitful men, nor do I consort with hypocrites;
5 I abhor the assembly of evildoers and refuse to sit with the wicked.
6 I wash my hands in innocence, and go about your altar, O Lord,
7 proclaiming aloud your praise and telling of all your wonderful deeds.
8 I love the house where you live, O Lord, the place where your glory dwells.
9 Do not take away my soul along with sinners, my life with bloodthirsty men,
10 in whose hands are wicked schemes, whose right hands are full of bribes.
11 But I lead a blameless life; redeem me and be merciful to me.
12 My feet stand on level ground; in the great assembly I will praise the Lord.
The Holy Bible : New International Version. 1996, c1984. Grand Rapids: Zondervan.