How/where to query phone spec from phone OS and build details
Fully endorse comments of Death Mask Salesman


  Is there an open data base that can be queried automatically?


One reliable database I am aware of is from Google itself

PDF at this link may help you List of all devices supported by Google Play

There is also .csv available 

Devices are listed in the following format - Marketing Name (Build.os.DEVICE/Build.os.MODEL) 

To get device specs , there are many sites - PDAdb is pretty good