After much searching I used the following code and it works fine:
$db->query();
$lastInsertedId = $db->insertid();