详解php封装db类连接sqlite3

小云云
小云云原创
2023-03-17 22:56:021575浏览

sqlite3_open是sqlite数据库的api函数(C/C++),作用是打开(或创建)一个数据库文件。本文主要和大家分享php封装db类连接sqlite3 的知识,希望能帮助到大家。

<?php
    class dbManager{
        public $db;
        function __construct(){
            if(!file_exists('./db.php')){
                $this->init();
                return;
            }
            $this->db = new SQLite3('./db.php');
        }
        function init(){
            $this->db = new SQLite3('./db.php');
            // TODO:
        }
        function changes(){
            return $this->db->changes();
        }
        function query($sql,$param=null,$memb=null){
            $stmt=$this->db->prepare($sql);
            if(!$stmt)
                return false;
            if($param){
                if(is_array($param)){
                    for($i=0;$i<count($param);$i++)
                        $stmt->bindValue($i+1,$param[$i]);
                }else{
                    $stmt->bindValue(1,$param);
                }
            }
            $rs=$stmt->execute();
            if(!$rs){
                $stmt->close();
                return false;
            }
            $arr=$rs->fetchArray(SQLITE3_NUM);
            $rs->finalize();
            $stmt->close();
            if(!$arr)
                return null;
            if(!$memb)
                return $arr;
            $res=array();
            for($i=0;$i<count($memb);$i++){
                $res[$memb[$i]]=$arr[$i];
            }
            return $res;
        }
        function queryAll($sql,$param=null,$memb=null){
            $stmt=$this->db->prepare($sql);
            if(!$stmt)
                return false;
            if($param){
                if(is_array($param)){
                    for($i=0;$i<count($param);$i++)
                        $stmt->bindValue($i+1,$param[$i]);
                }else{
                    $stmt->bindValue(1,$param);
                }
            }
            $rs=$stmt->execute();
            if(!$rs){
                $stmt->close();
                return false;
            }
            
            $res=array();
            while($arr=$rs->fetchArray(SQLITE3_NUM)){
                if(!$memb) {
                    $res[]=$arr;
                    continue;
                }
                if(count($memb)==1 && $memb[0]==null){
                    $res[]=$arr[0];
                    continue;
                }
                $it=array();
                for($i=0;$i<count($memb);$i++){
                    $it[$memb[$i]]=$arr[$i];
                }
                $res[]=$it;
            }
            $rs->finalize();
            $stmt->close();
            
            return $res;
        }
        function querySingle($sql,$param=null){
            $res=$this->query($sql,$param);
            if(!$res)
                return false;
            return $res[0];
        }
        
        function querySingleAll($sql,$param=null){
            $stmt=$this->db->prepare($sql);
            if(!$stmt)
                return false;
            if($param){
                if(is_array($param)){
                    for($i=0;$i<count($param);$i++)
                        $stmt->bindValue($i+1,$param[$i]);
                }else{
                    $stmt->bindValue(1,$param);
                }
            }
            $rs=$stmt->execute();
            if(!$rs){
                $stmt->close();
                return false;
            }
            
            $res=array();
            while($arr=$rs->fetchArray(SQLITE3_NUM)){
                $res[]=$arr[0];
            }
            $rs->finalize();
            $stmt->close();
            
            return $res;
        }
        function exec($sql,$param=null){
            $stmt=$this->db->prepare($sql);
            if(!$stmt)
                return false;
            if($param){
                if(is_array($param)){
                    for($i=0;$i<count($param);$i++)
                        $stmt->bindValue($i+1,$param[$i]);
                }else{
                    $stmt->bindValue(1,$param);
                }
            }
            $rs=$stmt->execute();
            if($rs) {
                $res=true;
                $rs->finalize();
            }else{
                $res=false;
            }
            $stmt->close();
            return $res;
        }
        
        function begin(){
            return $this->exec('BEGIN');
        }
        function rollback(){
            return $this->exec('ROLLBACK');
        }
        function commit(){
            return $this->exec('COMMIT');
        }
        
        function escapeString($s){
            return $this->db->escapeString($s);
        }
        //最新插入的id
        function lastInsertRowID(){
            return $this->db->lastInsertRowID();
        }
        
        function lastErrorMsg (){
            return $this->db->lastErrorMsg();
        }
    }

?>

PDO支持数据库移植,如果你的部署将来有多种数据库,那就用它了.同时,PDO是C设计的,执行效率较高.他已经封装为PHP的扩展库组件了.运行快,效率高

相关推荐:

Python中SQLite3的基本操作实例讲解

php使用pdo连接sqlite3的配置示例详解

PHP中调用sqlite3

以上就是详解php封装db类连接sqlite3 的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。