一直以來。咱們操做SQLITE,都是基於PHP默認的SQLITE操做模塊。php
今天介紹一個可以操做SQLITE2,和SQLITE3的PHP類,此爲我封裝的。sql
<?php abstract class Db{ public static function factory($type){ return call_user_func(array($type , 'getInstance')); } //查詢 public abstract function getArray($query); //運行SQL public abstract function exec($query); //獲取這次插入的ID public abstract function getInsertId($query); }網絡
class Sqlite extends Db{ private static $link = null; private $conn = null; private $pdo = null; private function __construct(){ $conf = Config::to(); $sqliteDsn = 'sqlite2:'.TM_PATH.'data'.DS.'csxyzs.db';//SQLITE文件路徑,假設在server上,請改用絕對路徑 $this->pdo = new PDO($sqliteDsn);//採用PDO操做 } public static function getInstance(){ if(is_null(self::$link)){ self::$link = new self(); } return self::$link; } public function getArray($query){ $return = array(); $rs = $this->pdo->query($query) or die(print_r($this->pdo->errorInfo(), true)); $return = $rs->fetchAll(); return $return; } public function exec($query){ $result = $this->pdo->exec($query) or die(print_r($this->pdo->errorInfo(), true)); return $result; } public function getInsertId($query){ $result = $this->pdo->exec($query); return $result; } }
$db = Db::factory('Sqlite'); $db->getArray('select * from table'); $db->exec('insert into table values("aa" , "bb")');
本人淘寶店地址:圖米網絡http://www.tome178.com or tome178.taobao.comfetch
假設要作企業站或模板站,請直接聯繫我。this