The story of the Temple's foundation is deeply tied to King David's personal sacrifices and his profound desire to create a resting place for the Divine Presence. Commentators offer different perspectives on the historical background of his prayer. One approach suggests that David composed this plea during a time of great crisis. While dressed in sackcloth alongside the elders of Israel during a devastating plague, the exact future location of the Temple was finally revealed to him at the threshing floor of Araunah the Jebusite. When an altar was built there and the plague stopped, David, after a lifetime of searching for this sacred spot, prayed that God would remember his tireless efforts [רד״ק, אבן עזרא, מאירי]. Alternatively, others view this prayer as looking forward to the next generation. Through divine inspiration, David foresaw that his son, King Solomon, would eventually face difficulties bringing the Ark of the Covenant into the newly built Holy of Holies. To prepare for this, David prayed in advance, hoping his own past merits would assist his son when that crucial moment arrived [מלבי״ם, חומת אנך].
Central to this prayer is a plea for God to remember David's deep suffering and affliction [אבן עזרא, ביאור שטיינזלץ]. This was not ordinary physical pain, but rather a severe emotional distress and immense personal burden. It reflects the strict vows he made, the sleep he lost, and the constant, restless worry he carried while trying to find a home for God and preparing the vast materials for its construction [רש״י, רד״ק, מצודת דוד].
Generally, righteous individuals do not ask for rewards for their good deeds. However, David's request was not for personal gain; it was entirely focused on ensuring that God's presence would successfully rest upon the Temple that Solomon would build [אלשיך]. Because the prophet Nathan had already informed David that he would not be allowed to build the structure himself, David asked God to merge his pure intentions and exhausting preparations with the physical construction. He hoped God would consider his groundwork as if he had built the Temple with his own hands [רד״ק, מאירי]. As a result of this absolute dedication, David earned the lasting honor of having the building of the Temple forever associated with his name [חומת אנך].