5

In one of my modules I try to connect to one SQlite DB to copy from it the info into my MySQL DB, however after creating the db info and trying to add them into the Drupal context, I get the error Notice: Undefined index: driver in Database::openConnection()

here is my function:

function query_247($table, $conditions = array()){
  $module_path = drupal_get_path('module', 'cw_ticketing' );
  $databases['247cinema']['db1'] = array (
    'driver' => 'sqlite',
    'database' => $module_path.'/247cinema_part1.sqlite',
  );
  $databases['247cinema']['db2'] = array (
    'driver' => 'sqlite',
    'database' => $module_path.'/247cinema_part2.sqlite',
  );
  // die(var_dump($databases));
  Database::addConnectionInfo('247cinema', 'db1', $databases);
  Database::addConnectionInfo('247cinema', 'db2', $databases);
  try {
    db_set_active('247cinema');
    if (count($conditions)) {
      //some logic
    }else{
      $where_s = false;
    }

    //some logic

    db_set_active();
    return$records;
  } catch (Exception $e) {
    $msg = $e->getMessage();
    watchdog('error', $msg);
    db_set_active();
  }
}

Anyone has any idea ?

3

The documentation is a bit misguiding as it states that the database array should be identical to the way it is declared on the settings.php file. This is wrong!

After digging a little bit the core module it shows that addConnectionInfo is expecting a array that defines the database and the driver.

Here is the correct implementation:

      $module_path = drupal_get_path('module', 'cw_ticketing' );
      $db1 = array (
        'database' => $module_path.'/247cinema_part1.sqlite',
        'driver' => 'sqlite',
      );
      $db2 = array (
        'database' => $module_path.'/247cinema_part2.sqlite',
        'driver' => 'sqlite',
      );
      // die(var_dump($databases));
      Database::addConnectionInfo('247', 'db1', $db1);
      Database::addConnectionInfo('247', 'db2', $db2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.