It is probable that David penned this psalm when he was persecuted by Saul; some passages in it agree particularly to the narrow escapes he had, at Keilah (1 Sam. 23:13), then in the wilderness of Maon, when Saul marched on one side of the hill and he on the other, and, soon after, in the cave in the wilderness of En-gedi; but that it was penned upon any of those occasions we are not told. It is a mixture of prayers, and praises, and professions of confidence in God, all which do well together and are helpful to one another. I. David professes his cheerful confidence in God, and, in that confidence, prays for deliverance out of his present troubles, Ps. 31:1-8. II. He complains of the very deplorable condition he was in, and, in the sense of his calamities, still prays that God would graciously appear for him against his persecutors, Ps. 31:9-18. III. He concludes the psalm with praise and triumph, giving glory to God, and encouraging himself and others to trust in him, Ps. 31:19-24.