`
haohappy2
  • 浏览: 326251 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

how to operate DB in drupal?

阅读更多
how to write and get connection in drupal?
  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.
how to  get the total?
     $total= db_reault(db_query(?,?)) 
how to get the list?
   $sql=?
   $result=db_query(db_rewrite_sql($sql));
   while($data=db_fetch_object($result)){
         $node=node_load($data->nid);
          print node_view($node,TRUE);
   }
how to get the range data in DB?
        $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
  
how to  make pagnation in drupal? as above we do like this.
        $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.
how to change the search on any place in drupal? 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.
0
1
分享到:
评论

相关推荐

    How 用法详解.doc

    3. 表示方式、手段或方法:询问具体的操作过程或方式时,"How"也常被使用,例如 "How do you operate this machine?" (你是如何操作这个机器的?) 或 "How did you solve the problem?" (你是如何解决这个问题的?) ...

    The Executive’s How-To Guide to Automation

    An algorithm is merely a set of rules, and anyone with the ability to envision how different components of a business can interact with other components already has the ability to work in algorithms....

    Social.Engineering.1523850930.epub

    The Art of Psychological Warfare, Human Hacking, Persuasion, and Deception Are You Ready To Learn How To Configure & Operate Cisco Equipment? If So You've Come To The Right Place - Regardless Of How ...

    Data Structures And Algorithms In Java 2nd PDF

    The programs demonstrate in graphical form what data structures look like and how they operate. In the second edition, the program is rewritten to improve operation and clarify the algorithms, the ...

    Particle Filters for Robot Navigation

    order to operate robustly, a robot needs to know what the environment looks like, where it is in its environment, and how to navigate in it. This work summarizes approaches that address these three ...

    Cryptography in .NET Succinctly(SyncFusion,2015)

    This book is aimed at .NET developers who want to understand how to use cryptography in their applications. The user is expected to be reasonably experienced as a .NET developer but no prior knowledge...

    UE(官方下载)

    This tutorial will show you how to access the information you need in your browser by simply highlighting your text in the edit window and clicking your toolbar button How to install UE3 UE3 is the ...

    Data Structures and Algorithms in Java (2nd Edition)

    The programs demonstrate in graphical form what data structures look like and how they operate. In the second edition, the program is rewritten to improve operation and clarify the algorithms, the ...

    OLE_DB_operate.rar_OLE DB_OLE数据库

    OLE DB,全称为Object Linking and Embedding Database,是微软公司推出的一种数据访问接口标准,旨在为应用程序提供统一的数据存取途径。OLE DB的核心理念是通过组件对象模型(COM)接口,让开发者能够轻松地访问...

    using ado.net to operate excel

    string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=exceldemo.xls;Extended Properties='Excel 8.0;HDR=YES;'"; ``` 对于Excel 2007及以上版本,应使用Microsoft.ACE.OLEDB.12.0提供程序: ...

    db_operate.tar.gz_sqilte lin

    在这个"db_operate.tar.gz_sqlite lin"压缩包中,包含的是一个针对SQLite3数据库操作的C语言实现,主要涉及到了对数据库的基本操作,如创建、打开、添加记录、查询、删除以及计算记录数量。下面将详细讲解这些知识点...

    Django Unleashed PDF 样章

    As you build your website, expert Django consultant and trainer Andrew Pinkham reveals how websites operate; how Django makes building websites easy; how to write Python code that leverages its ...

    camunda-operate-1.0.0.zip

    【camunda-operate-1.0.0.zip】是一个与Zeebe相关的压缩包,它提供了Zeebe的安装和快速入门指南。Zeebe是一个分布式工作流引擎,用于处理大规模的业务流程自动化任务。这个压缩包包含了部署Zeebe所需的所有组件,...

    ios-EFQRCode - A better way to operate two-dimensional code in Swift..zip

    EFQRCode 是一个用 Swift 编写的用来生成和识别二维码的库,它基于系统二维码生成与识别进行开发。 - 生成:利用输入的水印图/图标等资源生成各种艺术二维码; - 识别:识别率比 iOS 原生二维码识别率更高。

    Know Your Enemy:FFSN

    In this paper we will first provide an overview of what fast-flux service networks are, how they operate, and how the criminal community is leveraging them, including two types which we have ...

    multi_process_operate_db.zip

    在本项目"multi_process_operate_db.zip"中,我们将探讨如何在多进程中有效地操作SQLite数据库,以及在并发环境下可能出现的问题和解决方案。 首先,我们需要理解多进程的概念。在计算机编程中,多进程是指一个应用...

Global site tag (gtag.js) - Google Analytics