操作Sqlite数据库的类

操作Sqlite数据库的类



[复制到剪切板]
CODE:
<?php
/**
 * @name DbClass
 * @author gaoyuhong | qq 490997183 | email [email protected]
 * @copyright www.gg1st.cn
 * @使用方法
     $Db = DbClass::getInstance();
     $Db->loadConfig('sqlite:a.db');
     $Db->setTabel('admin');
     $Db->setField(array('id','name','password'));
     print_r($Db->doFecth());
     $Db->setCondition(' and id=1');
     print_r($Db->doFecthSigle());
     echo $Db->doUpdate(array("name"=>"'NewAdminName'"));
     echo $Db->doDelete();//注意,不带condition的delete将会清除数据表
     echo $Db->doInsert(array("name"=>"'myadmin'","password"=>"'123456'"));
     $Db->outDebug();
 */
  
class DbClass
  
{
   static 
$instance false;
   protected 
$connString;
   protected 
$Sql;
   protected 
$_tableName;
   protected 
$_ArrayFields;
   private function 
__construct(){
   }
   
/**
    * 生成DbClass单件
    *
    * @return DbClass
    */
   
public static function getInstance() {
    if (!
DbClass::$instance) {
     
DbClass::$instance = &new DbClass();
    }
    return 
DbClass::$instance;
   }
   
/**
   *设置连接字符串
   * @param string $ConnString
   * @example $Db->loadConfig('sqlite:a.sqlite');
   */
   
public function loadConfig($ConnString){
    if(isset(
$ConnString)){
     
$this->_connString $ConnString;
    }else{
     
$this->_connString null;
    }
   }
   
/**
   *获取连接字符串
   *@return string
   */
   
protected function getConnString(){
    return 
$this->_connString;
   }
   
/**
   *获取操作表名称
   *@return string
   */
   
protected function _getTable() {
     return 
$this->_tableName;
   }
   
/**
   *设置操作表名称
   *@param string $TableName
   */
   
public function setTable($TableName){
    if(isset(
$TableName))
    {
     
$this->_tableName $TableName;
    }
    else
    {
     
$this->_tableName null;
    }
   }
   
/**
   *获取操作字段
   *@return array
   */
   
protected function _getFields(){
    return 
$this->_ArrayFields;
   }
   
/**
   *设置操作字段
   *@param array $arrayField
   *@example $Db->setField(array('a','b','c')); 查询时用
   *@example $Db->setField(array("a"=>"'这里是插入测试'","b"=>"'插入字段2'"); 插入、更新时用
   */
   
public function setField($arrayFields){
    if(isset(
$arrayFields) && is_array($arrayFields)){
     
$this->_ArrayFields $arrayFields;
    }
    else{
     
$this->_ArrayFields null;
    }
   }
   
/**
   *获取查询条件
   *@return string
   */
   
protected function _getCondition(){
    return  
$this->_Condition;
   }
   
/**
   *设置查询条件,不带where
   *@param string $Condition
   *@example $Db->setCondition(" and a=1 and b='cc' ");
   */
   
public function setCondition($Condition){
    if(isset(
$Condition)){
     
$this->_Condition " where '1'='1' " $Condition ";";
    }else{
     
$this->_Condition null;
    }
   }
   
/**
   *清除查询条件
   */
   
public function cleanCondition(){
    
$this->_Condition null;
   }
   
/**
   *获取要执行的sql语句
   *
   *@return string;
   */
   
protected function _getSql(){
    return 
$this->Sql.$this->_getCondition();
   }
   
/**
   *设置要执行的SQl语句,要附加查询条件,请使用setCondition方法
   *@param string $Sql
   *@example $Db->setSql('select * from aa');
         $Db->setCondition(' and a=1 ');
   */
   
public function setSql($Sql){
    if (isset(
$Sql)) {
     
$this->Sql $Sql;
    }else {
     
$this->Sql null;
    }
   }
   
/**
   *调试,输出sql语句
   *@return string
   */
   
public function outDebug(){
    die(
$this->_getSql());
   }
   
/**
    * 数组转换为字符串
    *
    * @param array $array
    * @return String
    */
   
protected function _ArrayToString($array){
    if(
is_array($array)){
     return 
implode($array,",");
    }
   }
   
/**
    * PDO连接对象
    *
    * @return PDO
    */
   
protected function _getConn(){
    
$conn null;
    try {
     
$conn = &new PDO($this->getConnString());
    }catch (
PDOException $e){
     die(
$e->getMessage());
    }
    return 
$conn;
   }
   
/**
    * PDO记录集对象
    *
    * @return PDOStatement
    */
   
public function doQuery(){
    
$Query null;
    
$temp $this->_ArrayToString($this->_getFields());
    
$Sql="select $temp from ".$this->_getTable();
    
$this->setSql($Sql);
    try{
     
$Query  = &$this->_getConn()->query($this->_getSql());
    }catch(
PDOException $e){
     die(
$e->getMessage());
    }
    return 
$Query;
   }
   
/**
    * 记录集行数
    * @return int
    */
   
public function countRow(){
    try{
     
$Count = &count($this->doQuery()->fetchAll());
    }catch(
PDOException $e){
     die(
$e->getMessage());
    }
    return 
$Count;
   }
   
/**
    * 多条记录
    *
    * @return array
    */
   
public function doFetch(){
    
$Fetch null;
    try{
     
$Fetch = &$this->doQuery()->fetchAll();
    }catch(
PDOException $e){
     die(
$e->getMessage());
    }
    return 
$Fetch;
   }
   
/**
    * 单条记录
    *
    * @return array
    */
   
public function doFetchSigle(){
    
$Fetch null;
    try{
     
$Fetch = &$this->doQuery()->fetch();
    }catch(
PDOException $e){
     die(
$e->getMessage());
    }
    return 
$Fetch;
   }
   protected function 
doExec(){
      return 
$this->_getConn()->exec($this->_getSql()  ) ;
   }
   public function 
getLastId(){
    return 
$this->_getConn()->lastInsertId();
   }
   
/**
    * 插入操作
    *
    * @param array $arrayData
    */
   
public function doInsert($arrayData){
    
$this->cleanCondition();
    foreach(
$arrayData as $a=>$v)
    {
     
$temp[]= $a;
     
$tempv[] = $v;
    }
    
$Fields $this->_ArrayToString($temp);
    
$Values $this->_ArrayToString($tempv);
    
$Sql "insert into ".$this->_getTable() ."(".$Fields.")  values(".$Values.");";
    
$this->setSql($Sql);
    
$this->doExec();
    return 
$this->_getConn()->lastInsertId();
   }
   
/**
    * 更新操作,返回被印象行数
    *
    * @param Array $ArrayData
    * @return int
    */
   
public function doUpdate($ArrayData){
     foreach(
$ArrayData as $a=>$v)
     {
      
$temp[]="$a=$v";
     }
     
$tmp=$this->_ArrayToString($temp);
     
$Sql "update " $this->_getTable() . " set " $tmp;
     
$this->setSql($Sql);
     return 
$this->doExec();
   }
   
/**
    * 删除操作,返回被印象行数
    * @return integer
    */
   
public function doDelete(){
    
$this->setSql("delete from ".$this->_getTable().$this->_getCondition().";");
    return 
$this->doExec();
   }
   
/**
   *检查是否为同一条记录
   *
   *@return int
   */
   
public function checkSameRecord(){
    if(
$this->countRow()>1)
    {
     return 
false;
    }
    else
    {
     return 
$this->countRow();
    }
   }
  }
  
### 转载请注明出处 ###
?> ;

我的Blog

顶...~

很好!学习中!

学习

我晕,管理员太强了~~嘿
实在不好意思,自己没事时写个类,今天google里搜了下"www.gg1st.cn",竟然有帖子在这,呵呵
写的不好,大家多包含啊~~
现在的改进了,由xml配置,动态生成sql