当前位置: 首页 > 后端技术 > PHP

PHP包db类连接sqlite3

时间:2023-03-29 13:48:18 PHP

init();返回;}$this->db=newSQLite3('./db.php');}functioninit(){$this->db=newSQLite3('./db.php');//TODO:}functionchanges(){return$this->db->changes();}函数查询($sql,$param=null,$memb=null){$stmt=$this->db->prepare($sql);如果(!$stmt)返回假;if($param){if(is_array($param)){for($i=0;$ibindValue($i+1,$param[$i]);}else{$stmt->bindValue(1,$param);$rs=$stmt->execute();如果(!$rs){$stmt->close();返回假;}$arr=$rs->fetchArray(SQLITE3_NUM);$rs->finalize();$stmt->关闭();如果(!$arr)返回空;如果(!$memb)返回$arr;$res=数组();对于($i=0;$idb->prepare($sql);如果(!$stmt)返回假;if($param){if(is_array($param)){for($i=0;$ibindValue($i+1,$param[$i]);}else{$stmt->bindValue(1,$param);$rs=$stmt->execute();如果(!$rs){$stmt->close();返回假;}$res=array();while($arr=$rs->fetchArray(SQLITE3_NUM)){if(!$memb){$res[]=$arr;继续;}if(count($memb)==1&&$memb[0]==null){$res[]=$arr[0];继续;}$it=array();对于($i=0;$ifinalize();$stmt->关闭();返回$res;}函数querySingle($sql,$param=null){$res=$this->query($sql,$param);如果(!$res)返回假;返回$res[0];}函数querySingleAll($sql,$param=null){$stmt=$this->db->prepare($sql);如果(!$stmt)r变假;if($param){if(is_array($param)){for($i=0;$ibindValue($i+1,$param[$i]);}else{$stmt->bindValue(1,$param);$rs=$stmt->execute();如果(!$rs){$stmt->close();返回假;}$res=array();while($arr=$rs->fetchArray(SQLITE3_NUM)){$res[]=$arr[0];}$rs->finalize();$stmt->关闭();返回$res;}functionexec($sql,$param=null){$stmt=$this->db->prepare($sql);如果(!$stmt)返回假;if($param){if(is_array($param)){for($i=0;$ibindValue($i+1,$param[$i]);}else{$stmt->bindValue(1,$param);$rs=$stmt->execute();如果($rs){$res=true;$rs->finalize();}else{$res=false;}$stmt->close();返回$res;}functionbegin(){return$this->exec('BEGIN');}functionrollback(){return$this->exec('ROLLBACK');}functioncommit(){return$this->exec('COMMIT');}functionescapeString($s){返回$this->db->e??scapeString($s);}//最新插入的idfunctionlastInsertRowID(){return$this->db->lastInsertRowID();}functionlastErrorMsg(){return$this->db->lastErrorMsg();}}?>PDO支持数据库迁移,如果你的部署会有多个数据库,然后使用它。同时PDO是用C语言设计的,执行效率高。它已被打包为PHP的一个扩展库组件。运行速度快,效率高classdbManager{function__construct($config){$text=file_get_contents($config);$obj=json_decode($text);$this->db=newPDO($obj->dsn,$obj->user,$obj->pass);}privatefunctionbind_param($stmt,&$param){if($param||$param===0){if(is_array($param)){for($i=0;$ibindParam($i+1,$param[$i]);}else{$stmt->bindParam(1,$param);}}}functionexec($sql,$param=null){$stmt=$this->db->准备($sql);如果(!$stmt){var_dump($this->db->e??rrorinfo());返回假;}$this->bind_param($stmt,$param);$res=$stmt->execute();如果($res){$res=$stmt->rowCount();}$stmt->closeCursor();返回$res;}函数查询($sql,$param=null,$name=true){$stmt=$this->db->prepare($sql);如果(!$stmt){返回空值;}$this->bind_param($stmt,$param);$res=$stmt->执行();如果(!$res){$stmt->closeCursor();返回空值;}$arr=$stmt->fetch($name?PDO::FETCH_ASSOC:PDO::FETCH_NUM);$stmt->closeCursor();返回$arr;}functionqueryAll($sql,$param=null,$name=true){$stmt=$this->db->prepare($sql);如果(!$stmt){返回空值;}$this->bind_param($stmt,$param);$res=$stmt->执行();如果(!$res){$stmt->closeCursor();返回空值;}$arr=$stmt->fetchAll($name?PDO::FETCH_ASSOC:PDO::FETCH_NUM);$stmt->closeCursor();返回$arr;}函数querySingle($sql,$param=null,$name=true){$stmt=$this->db->prepare($sql);如果(!$stmt){返回空值;}$this->bind_param($stmt,$param);$res=$stmt->执行();如果(!$res){$stmt->closeCursor();返回空值;$arr=$stmt->fetchColumn();$stmt->closeCursor();返回$arr;}函数querySingleAll($sql,$param=null,$name=true){$stmt=$this->db->prepare($sql);如果(!$stmt){返回空值;}$this->bind_param($stmt,$param);$res=$stmt->执行();如果(!$res){$stmt->closeCursor();返回空值;}$arr=[];做{$item=$stmt->fetchColumn();如果($item)$arr[]=$item;}while($item!==false);$stmt->closeCursor();返回$arr;}publicfunctionbegin(){$this->db->beginTransaction();}publicfunctionrollback(){$this->db->rollBack();}publicfunctioncommit(){$this->db->commit();}}/*$db=newdbManager("db.json");$db->e??xec('CREATETABLEUSER(IDINTEGERPRIMARYKEYNOTNULL)');$db->e??xec('INSERTINTOuser(ID)VALUES(?)',1);var_dump($db->query('SELECT*FROMUSERWHEREID=?',1));var_dump($db->queryAll('SELECT*FROMUSERWHEREID=?',1));var_dump($db->querySingle('SELECT*FROMUSERWHEREID=?',1));var_dump($db->querySingleAll('SELECT*FROMUSERWHEREID=?',1));*/?>这是原生db类pdo版本导入修改后的json配置文件{"dsn":"sqlite:test.db","user":"","pass":""}我这里只是为了方便前端修改,也可以做成php文件