How do I attach an existing file to a node?
A few things..

You should avoid raw sql queries against entity tables, they are managed and it is not guaranteed that the structure will always break. Use entity queries and then load the entities using Node::loadMultiple() for example, or $storage->loadMultiple().

A few lines of your existing code are not necessary. the size is calculated, language() is a getter that has no arguments, it is automatically new.

To set the uid, you can use setOwner($user) or setOwnerId($uid), and there are corresponding getter methods on the node objects, so once you load them, you can just set it from the node with $file->setOwner($node->getOwner())

However, that seems to be a completely different question than what you have in the title. That just syncs the owner (which is not that important, not for public files at least). It doesn't do anything to connect them to the node. To get the usage tracking, you can rely on what Drupal does by default if you include the UUID as an data attribute. I don't remember the exact name right now, just manually add an image with CKEditor and you'll see it in the source code. If that's there then Drupal does it for you automatically.