⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlite.php

📁 a short sketch about linux syntex lines.
💻 PHP
字号:
<?php
/*
Sqlite 3.0数据库管理系统
*/
class Easy_Db_Sqlite implements Easy_Db_Interface {
	protected static $db;
	public $Cache= false;
	public $querys = 0;
	public function __construct(){
		$array = func_get_arg(0);
		try{
			self::$db = new PDO('sqlite:'.ROOT.$array['filename'], $array['user'], $array['pass']) or die ('error');
		}catch (PDOException  $e) {
			die( "Mysql Error!: " . $e->getMessage() . "<br />");
		}
	}
	/*
	$table 表名称
	$data 需要插入的数据数组
	成功返回插入记录的个数否则返回false
	*/
	public function insert($table, $data){
		$str = null;
		foreach($data as $key=>$value){
			$str.= $str ? ",`$key`" : "`$key`";
			$val.= $val ? ','.$this->quote(trim($value)) : $this->quote(trim($value));
		}
		$this->sql = "INSERT INTO $table($str) VALUES($val)";
		unset($table, $data);
		return $this->exec($this->sql);	//Return number of rows that were Insert
	}	//数据插入
	public function lastInsertId(){
		return sqlite_insert_id(self::$db);	//返回插入的记录ID
	}
	/*
	$table 表名称
	$where 删除记录的条件
	*/
	public function delete($table, $where = null){
		if (is_array($where)){
			foreach($where as $key=>$value){
				$wheres.= " AND `$key` = ".$this->quote($value);
			}
		}else{
			$wheres = 'AND '. $where;
		}
		$this->sql = "DELETE FROM $table WHERE 1=1 $wheres";
		unset($array, $wheres, $table, $debug, $params);
		$this->exec($this->sql);
		return $this->affected_rows();
	}		//删除数据
	/*
	数据更新
	$table 表名
	$data 更新数据的数组或者字符。键名为字段名键值为内容。
	$where 更新记录的条件。可以为数组或者字符
	*/
	public function update($table, $data, $where=null){//更新数据
		$str = null;
		$wheres = null;
		if (is_array($data)){
			foreach($data as $key=>$value){
				$str.= (empty($str)===false) ? ",`$key`=".$this->quote($value) : "`$key`=".$this->quote($value);
			}
		}else{
			$str = $data;
		}
		unset($key,$value);
		if (is_array($where)){
			foreach($where as $key=>$value){
				$wheres.= " AND `$key` = ".$this->quote($value);
			}
		}elseif (is_null($where)===false){
			$wheres = 'AND '. $where;
		}else{
			unset($wheres);
		}
		$this->sql = "UPDATE ".$table." SET $str WHERE 1=1 $wheres";
		unset($wheres);
		$this->exec($this->sql);
		return $this->affected_rows();	//返回影响的行数
	}
	/*
	获取记录的全部数据
	$sql  SQL语句
	$type  为查询记录的模式默认为sqlite_query 否则为sqlite_unbuffered_query
	*/
	public function fetAll($sql, $type=null){//取得数据作为数据
		$sth = $this->query($sql, $type);
		
		if($sth) {
			return($sth->fetchall(PDO::FETCH_ASSOC));
		}else{
			return array();
		}
		
	}			
	public function fetRow($sql, $type=null)			//取得一行数据作为数组
	{
		$sth = $this->query($sql, $type);
		return $sth->fetch(PDO::FETCH_ASSOC);
	}
	public function fetOne($sql){		//取得第一个字段值
	
	}
	/*
	返回记录总数
	*/
	public function getCount($sql){
		$this->sql =  preg_replace("/^select (.+?) from/is",  "SELECT COUNT(*) as total FROM", $sql);
		//echo $this->sql;
		$row = $this->fetRow($this->sql);
		return $row['total'];
	}
	public function query($sql, $type=null)		//执行一个SQL语句
	{
		$this->querys = $this->querys + 1;
		if (is_object($sql)===true){
			$this->sql = $sql->toString();
		}else{
			$this->sql = $sql;
		}
		try{
			$sth = self::$db->prepare($this->sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
			if (is_array($type)){
				$sth->execute($type);
			}else{
				$sth->execute();
			}
			return $sth;
		} catch (PDOException $e) {
			print_r($e);
			exit;
		}
	}
	public function affected_rows(){
		return sqlite_affected_rows(self::$db);
	}
	public function beginTransaction()	//开始事务处理
	{
		
	}
	public function commit()				//提交事务
	{
		
	}
	public function rollBack()			//事务失败回滚事务
	{
		
	}
	public function quote($string)		//转义字符
	{
		return '\''.sqlite_real_escape_string($string).'\'';
	}
	public function error($error)		//抛出字符信息
	{
		$config = $GLOBALS['config'];
		if( !$config['debug'] ) {
			$this->message = '<b>Errno:</b>'.self::$db->errorInfo().'<br/>';
			$this->message.= '<b>Error:</b>'.self::$db->errorCode().'<br/>';
			$this->message.= '<b>Query:</b>'.$this->sql.'<br />';
			$this->message.='<解决这个问题请到<a href="http://www.vodcms.com/bbs/">http://www.vodcms.com/bbs/</a>查找错误原因<br />';
			//throw new Exception($error.'SQL:'.$this->sql.'<br />请到<a href="">http://wwww.vodcms.com/bbs/</a>查找错误原因');
			echo $this->message;
		}else{
			throw new Exception('SQL:'.$this->sql.'<br />请到<a href="">http://wwww.vodcms.com/bbs/</a>查找错误原因');
		}
		exit;
	}
	public function debug($bool=false)				//开启调试
	{
		
	}
	public function select(){
		return new Easy_Db_Select();
	}
	/*
	用于执行更新操作SQL
	*/
	public function Exec($sql, $type=null){
	
		$this->sql = $sql;
		unset($sql);
		return $this->query($this->sql, $type);
	}
	/*
	列出指定表字段信息
	@param string $table 表名称
	*/
	public function list_fields($dbname, $table){
		return sqlite_list_fields($dbname, $table, self::$db) or die (sqlite_error());
	}
	public function showTable($dbname=null){
		if (is_null($dbname)){
			$dbname = $this->dbname;
			//$this->query('SHOW TABLES FROM '.$dbname);
			$data = array();
			foreach($this->fetAll('SHOW TABLES FROM '.$dbname) as $row){
				$data[] = current(array_values($row));
			}
		}
		return $data;
	}
	/**/
	public function version(){
		return sqlite_get_server_info(self::$db);
	}
	public function __call($method, $args){
		//$func = '__'.$method;
		//echo $func;
		if(method_exists($this, $func) === false){
			exit('调用了无效的类方法名:'.$method.'<br>错误行数:'.__LINE__.'<br>错误文件'.__FILE__);
		}else{
			return $this->$func($args);
		}
	}
	public function close(){
		self::$db = null;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -