一直以来,我们操作SQLITE,都是基于PHP默认的SQLITE操作模块。
今天介绍一个可以操作SQLITE2,和SQLITE3的PHP类,此为我封装的。
<?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文件路径,如果在服务器上,请改用绝对路径 $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.com
如果要做企业站或模板站,请直接联系我。
原文地址:http://blog.csdn.net/tomyjohn/article/details/39956533