how to operate DB in drupal?

本文深入解析如何在Drupal中进行数据库连接、使用DB抽象层、执行SQL查询,并提供数据库优化技巧,包括如何获取总条数、列表数据、范围数据,以及分页显示、搜索功能实现。此外,还介绍了如何连接多个数据库和改变搜索方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[b]how to write and get connection in drupal?[/b]
there is light DB abstract layer. you can go to the setting.php to see the connection of DB
sites/$website/setting.php
sites/default/setting.php
define like this $db_url='mysql://username:password@localhost/databasename'; if you use pg just repace mysql to pgsql.
use this DB abstract layer. you will not concern more about when you want to choose different DB
we choose db_query to make the Business layer to DB layer seperately. and we can see above. when $db_url choose mysql
it will choose different DB file. it means that when you choose mysql.it will choose database.mysqli.inc automaticly.
if you want to write a php code that don't related with drupal. you can do as follow:
1.include_once("include/bootstrap.inc") \
2.drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE)
3.last you can use db_query();
now we know how to get the connection and how to use it. so we can user "CRUDL" to operate the DB.as you can see follow
db_query('select * from {users} where uid=%d',$node->vid);
db_query("insert into {users}(nid,vid,punchine) values(%d,%d,'%s')",$node->nid,$node->vid,$node->punchine);
db_query("update ");
db_query("delete ");
you will see the table have bracket. this is very useful. we can set the prefix for those tables. we use this style that
will reduce the SQL inject. the follow one we will use more(%d,%s,%f,$b,%%)
%s means string ,%d means numberic,%b means binary. we should not put quote for this,%f means float ,%% use for serach faintly.
[b]how to get the total? [/b]
$total= db_reault(db_query(?,?))
[b]how to get the list?[/b]
$sql=?
$result=db_query(db_rewrite_sql($sql));
while($data=db_fetch_object($result)){
$node=node_load($data->nid);
print node_view($node,TRUE);
}
[b]how to get the range data in DB?[/b]
$type='blog'; $status=1;
$sql="select * from {node} n where type='%s' and status=%d order by n.created desc";
$result=db_query_range(db_rewrite_sql($sql),$type,$status,0,10); //db_query_range is just like the limit

[b] how to make pagnation in drupal? as above we do like this.[/b]
$type='blog'; $status=1;
$sql="select * from {node} n where type='%s' and status=%d order by n.created desc";
$result=pager_query(db_rewrite_sql($sql),$type,$status,0,10); //db_query_range is just like the limit
while($data=db_fetch_object($result)){
$node=node_load($data->nid);
print node_view($node,TRUE);
}
print theme('pager',null,10). don't need to pass the total of result. just pass the $result.
if you want to deal more Data in DB. you need to user the temporary table and the ways.
db_query_temporary($sql,$arguments,$tempoaray_table_name); and we can create a table specially.
P.S you should use bracket to temporary table.
[b] how to change the search on any place in drupal? [/b]we can use hook. if you want to other one change the DB.
you need to use db_write_sql(); hook_db_rewrite_sql. if we want rewrite the some moule search
function user_db_rewrite_sql($query,$primary_table,$primary_field,$args){
switch($primary_filed){
case 'nid':
if(!user_access('administrator content')){
$array=array();
}
if($primary_table=='n'){
$array['where']="(n.moderate=0)";
}elseif(preg_match('@{node}([A-Za-z_]+)@',$query,$match)){
$node_table_alias=$match[1];
$array['join']="left join {node} n on $node_table_alias.nid=n.nid";
$array['where']="($node_table_alias.moderate=0)";
}
return $array
}
}
if we don't use hook. it will like this select * from {node} n where n.type='blog' and n.status=1
then we use it will change like this select * from {node} n where n.type='blog' and n.status=1 and n.moderate=0
don't do like this select * from node as n.comment as c where n.nid=c.nid. the db_write_sql can't understand it.
how to connect many DB?
as we see above. we have seen the $db_url='mysql://username:password@localhost/databasename'.actually,the $db_url
is a array. and it's defuault if only have one. so we can do like this.
$db_url['db1']='mysql://username:password@localhost/databasename';
$db_url['default']='mysql://username:password@localhost/databasename'; this is drupal's default db connection.
if we want to get this connect, we just db_set_active('db1'). that's so easy. if we want back. just db_set_active('default');
we can't use mysql and pgsql or some other db together. but we can use mulply mysql or pgsql or some other db.
if we want do use different type of DB. you can refer http://drupal.org/node/1952
how to install and change the DB in drupal?
we can use module .install function. and the DB type we can get from tht $GLOBALS['db_type']. and we user hook_install
function book_install(){
switch($GLOBALS['db_type']){
case 'mysql':
case 'mysqli':
db_query('create sql'
/*!40100 DEFAULT CHARACTER SET UTF8*/ this mean mysql version is above 4.01 that will excute this code
);

}
}
when you want to change the db schema. you should update your install file. or write a update function like this.
the order should be added it.
function book_update_1(){ //the second book_update_2..
$items=array();
$items[]=update_sql("alter table {book} add column rank int_unsigned not null default'0'");
}
you can use http://yourwebsite/update.php to run it. as we mention before. we can use devel and delete the related data in
system table. when you uninstall it. you just delete the related table and varibles.
how to write you own abstract DB layer?
we call it DNAbase.. just copy includes/database.mysql.inc and rename it withe includes/database.dnabase.inc.
when we use it. just change $db_url mysql to dnabase.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值