dB is a logarithmic measure of power, usually relative (for instance, the gain of an amplifier) or 'absolute' when you reference it to a given unit (for instance dBm, when you compare a given electric power refered to a miliwatt)
The difference between them is only an additive factor, which is the reference
In your calculation, if buffer contains a wave amplitude which can take both positive and negative values, these values must always be squared (power is proportional to the square of the amplitude), otherwise the resulting sum could be null.
also, when calculating dBs from a dynamic input, which I guess is the case, the way to proceed is by a sliding a time window and/or time segments which include N samples.
hope it helps ?