1 Samuel Ch 27 tells of David living in exile among the Philistines, but also attacking certain areas nearby.It makes tough reading in parts, and I cannot rationalise those passages, although later in The Bible, David is forbidden to help build the Temple as he fought in too many wars.