How db_set_active works with other db? Is db_set_active() need to work with a Drupal database or can I use it with a database that doesn't contain any drupal tables ?
db_set_active() is used for connecting to another database. The other database does not need to be Drupal at all.

Just don't forget to switch back to default DB when you are done. If you don't, Drupal will try to perform all subsequent queries in the other database, which will result in a lot of errors and potentially mess up the data in the other database.

Here's example code:

// Connect to your other database.
db_set_active('your_other_db');
// Run any queries you want.
db_query("...");
// Switch back to the default database of your installation.
db_set_active();


You can read more about db_set_active() here.

Another note, per Neograph734's reminder: the argument you would pass to the db_set_active() needs to be the same key you used for adding another DB access info in settings.php file.

Here's an example:

$databases = array (
  'default' =&gt; array (
    'default' =&gt; array (
      'database' =&gt; 'your_default_database',
      'username' =&gt; 'username',
      'password' =&gt; 'password',
      'host' =&gt; 'localhost',
      'port' =&gt; '',
      'driver' =&gt; 'mysql',
      'prefix' =&gt; '',
    ),
  ),
  'your_other_db' =&gt; array (
    'default' =&gt; array (
      'database' =&gt; 'your_other_database',
      'username' =&gt; 'username',
      'password' =&gt; 'password',
      'host' =&gt; 'localhost',
      'port' =&gt; '',
      'driver' =&gt; 'mysql',
      'prefix' =&gt; '',
    ),
  ),
);