How to connect to multiple databases within Drupal

本文介绍如何在Drupal中配置和使用多个数据库,包括设置默认数据库连接、动态添加数据库连接及执行跨库查询的方法。

Drupal can connect to different databases with elegance and ease!

First define the database connections Drupal can use by editing the $db_url string in the Drupal configuration file (settings.php for 4.6 and above, otherwise conf.php). By default only a single connection is defined

 

<?php
$db_url
= 'mysql://drupal:drupal@localhost/drupal';
?>

 

To allow multiple database connections, convert $db_url to an array.

 

<?php
$db_url
['default'] = 'mysql://drupal:drupal@localhost/drupal';
$db_url['mydb'] = 'mysql://user:pwd@localhost/anotherdb';
$db_url['db3'] = 'mysql://user:pwd@localhost/yetanotherdb';
?>

 

Note that database storing your Drupal installation should be keyed as the defaultconnection.

To query a different database, simply set it as active by referencing the key name.

 

<?php
db_set_active
('mydb');

db_query('SELECT * FROM table_in_anotherdb');

//Switch back to the default connection when finished.
db_set_active('default');
?>

 

Make sure to always switch back to the default connection so Drupal can cleanly finish the request lifecycle and write to its system tables.

Note: It is particularly important to switch back to the active Drupal database prior to any calls to Drupal functions. Errors in the error log about not being able to find the 'system' table are an indication that calls to Drupal functions preceed switching back to the default database.

This only works with two databases of the same type. For example the following codewill not work.

<?php
// ... header of the settings.php file

$db_url = array (
"default" => "mysql://user:pass@host/db",
"second" => "pgsql://user:pass@host/db"
);

// ...
?>

Set up multiple database on the fly
<?php
global $db_url; // the internal variable that contains database link
if (!is_array($db_url)) {
 
$default_db = $db_url;
 
$db_url = array('default' => $default_db);
}
//set up the new database value
$db_url['mydb'] = 'mysql://user:pwd@localhost/anotherdb';

db_set_active('mydb');    // activation & execution same as explained above
$results = db_query($sql); //sql represents the query to be executed
db_set_active('default'); // set back to original
?>

 

Drupal 7

In Drupal 7, first construct a database connection array, then connect to it to Drupal's front-controller, then execute queries using D7's database abstraction layer.

 

<?php
  $other_database
= array(
     
'database' => 'databasename',
     
'username' => 'username', // assuming this is necessary
     
'password' => 'password', // assuming this is necessary
     
'host' => 'localhost', // assumes localhost
     
'driver' => 'mysql', // replace with your database driver
 
);
 
// replace 'YourDatabaseKey' with something that's unique to your module
 
Database::addConnectionInfo('YourDatabaseKey', 'default', $other_database);
 
db_set_active('YourDatabaseKey');

 
// execute queries here

 
db_set_active(); // without the paramater means set back to the default for the site
 
drupal_set_message(t('The queries have been made.'));
?>

 

For a complete reference of all Drupal Database Abstraction Layer functions seehttp://api.drupal.org/api/group/database.

【源码免费下载链接】:https://renmaiwang.cn/s/qqeui ### Python通过Matplotlib生成复合饼图在数据分析与可视化领域,图表是一种强大的工具,能够帮助我们更好地理解数据、发现模式并做出决策。其中,饼图因其直观性而在展示部分与整体的关系时尤为常见。然而,在某些情况下,单一的饼图可能无法完全展现复杂的数据结构或层级关系。这时,复合饼图(也称为嵌套饼图或多层饼图)就派上了用场。#### 复合饼图的概念复合饼图是一种特殊的饼图形式,它将一个或多个较小的饼图嵌入到一个较大的饼图中,以此来表示数据的不同层次或类别。这种图表非常适合用来展示多层次的数据结构,比如市场份额中的细分市场占比等。#### Matplotlib简介Matplotlib是一个用于Python的2D绘图库,它可以生成各种类型的图表,包括线图、柱状图、散点图、饼图等。由于其高度的自定义能力和灵活性,Matplotlib成为了数据科学家和工程师们进行数据可视化的主要工具之一。#### 使用Matplotlib绘制复合饼图下面我们将详细介绍如何使用Matplotlib库在Python中生成复合饼图。1. **导入必要的库** ```python import matplotlib.pyplot as plt from matplotlib.patches import ConnectionPatch ```2. **创建画布和子图** 在复合饼图中,通常会有一个主饼图和一个或多个子饼图。因此,我们需要创建一个包含这些子图的画布。 ```python fig = plt.figure(figsize=(9, 5.0625)) ax1 = fig.add_subplot(121) # 主饼图所在的子图 ax2 = fig.add_subplot(122) # 子饼图所在的子图 fig.
【源码免费下载链接】:https://renmaiwang.cn/s/u0npk 在Java程序设计语言中,将字符串内的字符按字母顺序重新排列是一种常见的操作,在处理文本数据或需要对字母顺序进行排序的场景中尤为常见。本节内容将详细讲解如何实现这一功能。由于Java字符串结构固定,无法对其进行后续更改,因此在对字符串中的字符进行排序时必须采取特殊方式。为达到排序目的,我们需要理解以下关键点:首先,Java字符串是不可变对象,默认由`String`类创建;其次,在这种类型下无法直接修改原有内容,因此实现字符排序需要通过构造新的字符串对象来完成。 具体步骤如下: 1. **转换字符数组**:利用`toCharArray()`方法将原始字符串转换为可操作的字符数组。 2. **排序字符数组**:调用`Arrays.sort()`方法对上述生成的字符数组进行排序,默认按Unicode值排列,对于a-z范围内的字母顺序与Unicode排序一致。 3. **构造新字符串**:通过`new String(charArray)`或`String.valueOf(charArray)`将已排序的字符数组转换为新的字符串对象。 以下示例代码展示了这一操作的具体实现: ```javaimport java.util.Arrays;public class Zifuchuan { public static void main(String[] args) { String originalStr = "java 字符串a-z排序"; // 步骤1:将字符串转换为字符数组 char[] chars = originalStr.toCharArray(); // 步骤2:对字符数组进行排序 Arrays.sort(chars); // 步骤3:构建新的排序后的字符串 String sortedStr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值