In a monarchy, it is always big news when the king is sick. Uncertainty grips the kingdom. If no one sees the king’s face, rumors quickly take his place.
This seems to be the setting of this Psalm. David was sick and his enemies seized their opportunity to sew seeds of doubt in the kingdom.
David begins with a description of God’s compassion and care for the poor, sick, and needy. He then describes the treatment he is receiving, not just from his enemies, but from those who he considered to be his friends and allies. He is contrasting the two. He felt betrayed.
Jesus quoted this Psalm in reference to the betrayal of Judas. Could it be that King David was going through all of this, this sickness and betrayal, to serve as an opportunity to prophecy the betrayal of the King of Kings? Who knows what purpose our suffering may serve!