How to resolve the error "Base table or view not found"?
You need to switch the database back before it carries out any operation that requires the Drupal database.

// Switch to your custom database.
db_set_active('mykey');

// Work with that database (select/insert/update).
...

// Switch back, before you use the Drupal API again
db_set_active();