search
Homephp教程php手册PHP操作SQLITE

PHP操作SQLITE

Jun 06, 2016 pm 08:00 PM
phpsqlisqliteusoperate

一直以来,我们操作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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)