Selecting one result from database I am storing some data into the database


try {
      $fields = array(
        'uid' =&gt; \Drupal::currentUser()-&gt;id(),
        'value' =&gt; $form_state-&gt;getValue('blood_sugar'),
        'created' =&gt; time(),
      );
      Database::getConnection()-&gt;insert('blood_sugar_monitor')-&gt;fields($fields)-&gt;execute();
}


Later on I would like to check the latest inserted row to check if it was inserted within the last hour.

Could someone provide an example of fetching one item from the database?
You can follow the Database Abstraction API documentation. Although most of that documentation points to using the deprecated methods. You may find the current classes and methods, by clicking into each function that you need (such as db_select), and reading the deprecated messages.

This is very similar to what you have done for inserts (using Dynamic Query).

$id = Database::getConnection()-&gt;select('blood_sugar_monitor')
  -&gt;fields('blood_sugar_monitor', ['my_primary_key'])
  -&gt;range(0, 1)
  -&gt;orderBy('my_primary_key', 'DESC')
  -&gt;execute()
  -&gt;fetchField(0);