zoukankan      html  css  js  c++  java
  • CI数据库操作_查询构造器类

    =================数据库操作======================
    1.数据库配置:
    config/database.php 用户名 密码 数据库

    2 加载数据库类:$this->load->database();
    也可以自动加载:在$autoload['libraries']中添加值"database"

    3 使用:$this->db->...

    * 连接多个数据库:
    1)在database.php中配置多个数据库连接信息:
    $db["default"] = array();
    $db["database2"] = array();...

    2) 在连接数据库时传入索引("default"或"database2")
    3) $obj = $this->load->database("default",true) ,注意第二个参数在操作多个库时必须写,返回为连接的标示
    4)使用数据库操作时,使用$obj->....进行操作


    * 如果数据库表中指定了数据库表前缀,那么可以通过$this->db->dbprefix("表名")得到完整的表名
    如:$sql = "select * from ci_user" --> "select * from ".$this->db->dbprefix("user");


    4.执行语句:$this->db->query("语句");
    如果用占位符方式,则query("语句",参数);*多个参数的时候需要使用数组
    --查询绑定
    * $sql = "select * from user where uid =? and pwd=?";
    $this->db->query($sql,array("lily",123));

    * $sql = "select * from user where uid in ? and XXX=?";
    $this->db->query($sql,array(array(1,3,4),1));

    select:返回一个查询结果集
    insert/delete/update:返回true/false

    查询:方法:query()//返回对象
    二维数组:$res->result()
    二维数组:$res->result_array()
    返回一行数据:$res->row()/$res->row(行索引)
    返回一行数据:$res->row_array()

    5.常用辅助函数:
    * $this->db->insert_id();返回自增序列的id
    * $this->db->affected_rows();返回受影响的行数
    * $this->db->last_query();返回最后一次执行的sql语句
    * $this->db->count_all(table);返回表中有多少条数据
    * $this->db->insert_string("表名",data);将data插入到表中 -- 生成sql语句
    * $this->db->update_string("表名",data,condition);根据condition修改表中的data数据 -- 生成sql语句


    =============查询构造器==================
    一、 查询:
    1.$this->db->get(表名,限定行数,起始值)
    查询数据表中的数据,返回查询结果集,等同于$this->db->query("sql");

    2.$this->db->get_where(表名,where,limit,offset)
    根据指定条件进行查询

    3.$this->db->select(fiels);
    指定要查询出哪些字段,多个字段之间用,分隔,第二个参数设置是否保护字段名,默认为true

    4.连贯操作:
    $this->db
    ->select("字段") //用","隔开
    ->from("表名")
    ->where("查询条件")
    1)可以字符串,
    2)也可以使用数组array("字段1"=>1,"字段2"=>'zx')-and相连的条件
    3)where("字段名",$id)//字段名=$id.如果需要不等于:where("字段名<>",$id)
    ->or_where("查询条件")//使用同where,当传递数组时用or连接
    ->order_by("排序")
    ->group_by("分组")
    ->having("分组后的查询条件")
    ->limit(n,m)//n m和数据库中的limit是相反的:"limit m,n"
    ->join("要连接的表名","连接的条件(on....=...)","可选参数:连接方式-left right,默认内连接")
    ->get(); //执行
    * get以上的方法都是不需要顺序,建议大家按sql的语句
    * 复杂的语句建议使用query()的方法

    ----------------------------------

    二、 添加:
    页面显示----添加表单
    $this->load->view("insert");
    添加的操作
    模型:$this->db->insert("user",$arr);//表名,添加的数据(数组:字段=>值)
    返回值:影响行?true/false
    控制器:
    看表单传递的数据:$this->input->post()
    调用模型层中的方法

    批量插入:
    $this->db->insert_batch(tableName,data) -- data可以为二维数组,表示多条数据
    返回受影响的行数

    ----------------------------------

    三、修改:
    /*
    从列表跳转到修改页面
    页面显示:当前数据的内容(表单)
    模型:查询当前这条数据(需要给定参数id)
    控制器: 1)加载模型
    2)获取参数:$this->uri->segment(数字)//参数从index.php之后开始数1,以此类推
    例:http://localhost/ci/index.php/user/update/id/1
    //希望取到id后的1 ,$this->uri->segment(4)
    3)调用模型层中的方法->显示
    不存在数据的时候:提示,跳转地址:site_url("控制器/方法")
    */

    $this->db->update(tableName,data,where); -- data为一个数组,where可以是一个字符串
    $this->db->replace(tableName,data); -- data中必须包含主键或唯一索引,而且数据表中的所有数据都必须出现在data中

    ----------------------------------

    四、 删除:
    $this->db->delete(tableName,where);//表名,where条件(可以数组,也可以字符串)
    $this->db->empty_table(tableName);//删除表中的所有数据,id不会重置,等同于delete
    $this->db->truncate(tableName);//清空表,并重置id

    ----------------------------------

    五、set()
    $this->db->set("key","value") 或 $this->db->set(["key"=>"value"])

    例:$this->db->set("name","lily")->where()->update(tableName);
    * insert/update/delete都可以使用

  • 相关阅读:
    zookeeper使用场景
    zookeeper安装配置
    hadoop 远程调试
    deep learning笔记
    Sentiment Analysis(1)-Dependency Tree-based Sentiment Classification using CRFs with Hidden Variables
    PRML阅读笔记 introduction
    Python 学习笔记(2)
    python nltk 学习笔记(5) Learning to Classify Text
    python nltk 学习笔记(4) Writing Structured Programs
    python nltk 学习笔记(3) processing raw text
  • 原文地址:https://www.cnblogs.com/-xiepan/p/7722239.html
Copyright © 2011-2022 走看看