Add value to a field from database query

I have created a field in customer doctype called “Total Loyalty Points”.
And it will be a read only field where the value is to be retrieved from the database.
Basically I want the total loyalty points acquired by a customer and not the net loyalty points(which is loyalty points-redemption) which is already available. So I opened my database and wrote the query:
select sum(loyalty_points) into @totalpoints from tabLoyalty Point Entry where customer='Abraham Tamuri' and loyalty_points>0;
So i got the total points and saved it into a variable called @totalpoints. Now how do I bring this value to my field created in Customer?
Do I write a custom script or do I have to write a code in the .py file?



But that is not what I want. I need to take a value from a certain condition using the query.