php封装db类连接sqlite3数据库的方法实例

SQLite3扩展名在PHP 5.3.0+以上都会默认启用。可以在编译时使用–without-sqlite3来禁用它。

Windows用户可通过启用php_sqlite3.dll才能使用此扩展。 php_sqlite3.dll默认包含在PHP 5.3.0之后的PHP发行版中。

有关详细的安装说明,请查看PHP教程及其官方网站。

本文主要介绍了关于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的扩展库组件了.运行快,效率高
原文链接:https://segmentfault.com/a/1190000012472350
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容