Do you think it is impossible to express two different thoughts at two different times. They are completely different contexts.
Psalm 51 is a psalm of deep contrition and sorrow with repentance.
Psalm 139 is a psalm of praise and amazement at God's creation, particularly the human body. Have you never done the same thing?
Have you ever repented of sin in your life?
Have you ever gave the Lord thanks for your life, including the fact that your heart pumps without you having to think about it?
First of all, if you substitute words in Psa 51:5, it immediately becomes clear that this verse is describing the actions of his mother, not David.
Psa 51:5 Behold, I was shapen in iniquity; and in sin did my mother conceive me.
Now substitute some words and look what it says.
Behold, I was
beaten in
anger; and in
wrath did my mother
scourge me.
That may seem silly, but it clearly demonstrates this verse is speaking about David's mother, not David.
And as I wrote earlier, there is some controversy concerning her. There is something about her that caused Jesse and his other seven sons to be ashamed of David and treat him very poorly. It might be because she had had relations and children with Nahash the Ammonite, or it is possible David was conceived out of wedlock, and that Jesse had to marry her.
But there is scripture to support that David's seven older brothers were born to another mother, they were tall and very handsome, while David was very short and not "goodly" to look at.
1 Sam 16:5 And he said, Peaceably: I am come to sacrifice unto the LORD: sanctify yourselves, and come with me to the sacrifice. And he sanctified Jesse and his sons, and called them to the sacrifice.
6 And it came to pass, when they were come, that he looked on Eliab, and said, Surely the LORD'S anointed is before him.
7 But the LORD said unto Samuel,
Look not on his countenance, or on the height of his stature; because I have refused him: for the LORD seeth not as man seeth; for man looketh on the outward appearance, but the LORD looketh on the heart.
8 Then Jesse called Abinadab, and made him pass before Samuel. And he said, Neither hath the LORD chosen this.
9 Then Jesse made Shammah to pass by. And he said, Neither hath the LORD chosen this.
10 Again, Jesse made seven of his sons to pass before Samuel. And Samuel said unto Jesse, The LORD hath not chosen these.
11 And Samuel said unto Jesse, Are here all thy children? And he said, There remaineth yet the youngest, and, behold, he keepeth the sheep. And Samuel said unto Jesse, Send and fetch him: for we will not sit down till he come hither.
12 And he sent, and brought him in.
Now he was ruddy, and withal of a beautiful countenance, and goodly to look to. And the LORD said, Arise, anoint him: for this is he.
Again, Samuel came to Jesse and requested to see all his sons. They made a feast and Jesse brought only his seven oldest sons. They were tall and handsome, but God did not pick any of these men to be the king.
Samuel asks to see the sons again, and again Jesse presents only his seven oldest sons, but none of them are God's chosen.
Finally Samuel asks if there are any more sons, and Jesse admits that he has one more son, the youngest, and that he has been left keeping the sheep.
This shows that Jesse was ashamed of David and did not want to present him to Samuel. But as we know, David, though rejected by his own father and brothers, was the man God chose to be king of Israel.
So, David was the black sheep. There was some shame attached to his mother, and many scholars believe this is what he was expressing in Psa 51:5. He was simply confessing he was the lowly "dog" his own family had always said he was.