Home  >  Article  >  php教程  >  PHP操作SQLITE

PHP操作SQLITE

WBOY
WBOYOriginal
2016-06-06 20:00:02986browse

一直以来,我们操作SQLITE,都是基于PHP默认的SQLITE操作模块。 今天介绍一个可以操作SQLITE2,和SQLITE3的PHP类,此为我封装的。 ?phpabstract class Db{public static function factory($type){return call_user_func(array($type , getInstance));}//查询

一直以来,我们操作SQLITE,都是基于PHP默认的SQLITE操作模块。

PHP操作SQLITE

今天介绍一个可以操作SQLITE2,和SQLITE3的PHP类,此为我封装的。

<?php abstract class Db{
	
	public static function factory($type){
		return call_user_func(array($type , &#39;getInstance&#39;));
	}
	//查询
	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")');

不过如果要使用PDO,请确保你的服务器支持如下显示。

PHP操作SQLITE


本人淘宝店地址:图米网络http://www.tome178.com or tome178.taobao.com

如果要做企业站或模板站,请直接联系我。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn