📄 sqlite.pdo.php
字号:
<?php
class sqlite {
var $dbPath;
var $connId;
var $error;
var $errorMessage;
var $readOnly;
var $resId;
var $libversion;
var $dbType;
function sqlite($dbPath) {
$this->dbPath = $dbPath;
}
function isReadOnly() {
return $this->readOnly;
}
function libversion() {
return $this->libversion;
}
function opendb() {
if(is_resource($this->connId) || is_object($this->connId)) {
return $this->connId;
} else {
return false;
}
}
function escape($string) {
return str_replace("'", "''", $string);
}
function getDbType(){
$fp = @fopen($this->dbPath, 'r');
if ($fp){
$signature = fread($fp, 47);
if($signature == "** This file contains an SQLite 2.1 database **") $dbType = "SQLite2";
elseif(substr($signature, 0, 15) == "SQLite format 3") $dbType = "SQLite3";
else $dbType = false;
fclose($fp);
return $dbType;
}
}
}
class sqlite2 extends sqlite {
function sqlite2($dbPath) {
$this->dbType = "SQLite2";
if($dbPath == ':memory:') {
$this->readOnly = false;
} else {
$this->readOnly = !is_writable($dbPath);
}
parent::sqlite($dbPath);
$this->connect($dbPath);
}
function opendb($dbPath) {
if(DEBUG) {
if($dbPath == ':memory:') $this->connId = sqlite_popen($dbPath, 0666, $this->error);
else $this->connId = sqlite_open($dbPath, 0666, $this->error);
} else {
if($dbPath == ':memory:') $this->connId = @sqlite_popen($dbPath, 0666, $this->error);
else $this->connId = @sqlite_popen($dbPath, 0666, $this->error);
}
return $this->connId;
}
function getError($errorCode = null) {
if(!$this->error) $this->error = sqlite_last_error($this->connId);
if($errorCode == null) $errorCode = $this->error;
$this->errorMessage = sqlite_error_string($errorCode);
return $this->errorMessage;
}
function getErrorMessage() {
return $this->errorMessage;
}
function query($sqlString, $buffered = true, $assign = true) {
//echo "query = $sqlString<br>";
if(substr(trim($sqlString), -1) != ';') $sqlString .= ';';
if($buffered) {
if(DEBUG) $resId = sqlite_query($this->connId, $sqlString);
else $resId = @sqlite_query($this->connId, $sqlString);
} else {
if(DEBUG) $resId = sqlite_unbuffered_query($this->connId, $sqlString);
else $resId = @sqlite_unbuffered_query($this->connId, $sqlString);
}
if($assign) $this->resId = $resId;
return $resId;
}
function array_query($sqlString, $result_type=SQLITE_BOTH, $decode_binary=true) {
if(DEBUG) return sqlite_array_query($this->connId, $sqlString, $result_type, $decode_binary);
else return @sqlite_array_query($this->connId, $sqlString, $result_type, $decode_binary);
}
function num_rows($resId = null) {
if($resId == null) $resId = $this->resId;
if(DEBUG) $out = sqlite_num_rows($resId);
else $out = @sqlite_num_rows($resId);
return $out;
}
function fetch_single($resId=null, $result_type=SQLITE_BOTH) {
if($resId == null) $resId = $this->resId;
if(DEBUG) $out = sqlite_fetch_string($resId,$result_type);
else $out = @sqlite_fetch_string($resId,$result_type);
return $out;
}
function fetch_array($resId=null, $result_type=SQLITE_BOTH,$decode_binary=true) {
if($resId == null) $resId = $this->resId;
if(DEBUG) $out = sqlite_fetch_array($resId,$result_type, $decode_binary);
else $out = @sqlite_fetch_array($resId,$result_type, $decode_binary);
return $out;
}
function last_insert_id() {
return sqlite_last_insert_rowid($this->connId);
}
function changes() {
if(DEBUG) $out = sqlite_changes($this->connId);
else $out = @sqlite_changes($this->connId);
return $out;
}
function num_fields($resId = null) {
if($resId == null) $resId = $this->resId;
if(DEBUG) $out = sqlite_num_fields($resId);
else $out = @sqlite_num_fields($resId);
return $out;
}
function field_name($resId = null, $index) {
if($resId == null) $resId = $this->resId;
if(DEBUG) $out = sqlite_field_name($resId, $index);
else $out = @sqlite_field_name($resId, $index);
return $out;
}
function create_function($function_name, $callback, $num_arg=null) {
if(DEBUG) return sqlite_create_function($this->connId, $function_name, $callback, $num_arg);
else return @sqlite_create_function($this->connId, $function_name, $callback, $num_arg);
}
function create_aggregate($function_name, $step_func, $finalize_func, $num_args=null) {
if(DEBUG) return sqlite_create_aggregate($this->connId, $function_name, $step_func, $finalize_func, $num_args);
else return @sqlite_create_aggregate($this->connId, $function_name, $step_func, $finalize_func, $num_args);
}
function sqlite_version() {
return sqlite_libversion();
}
function close() {
if(DEBUG) return sqlite_close($this->connId);
else return @sqlite_close($this->connId);
}
function sqlitem_busy_timeout($milliseconds=0) {
if(DEBUG) $out = sqlite_busy_timeout($this->connId, $milliseconds);
else $out = @sqlite_busy_timeout($this->connId, $milliseconds);
return $out;
}
function beginTransaction() {
$this->query('BEGIN TRANSACTION;', false, false);
}
function commitTransaction() {
$this->query('COMMIT TRANSACTION;', false, false);
}
function rollBackTransaction() {
$this->query('ROLLBACK TRANSACTION;', false, false);
}
}
if(!defined('PDO_ATTR_TIMEOUT')) define('PDO_ATTR_TIMEOUT', PDO::ATTR_TIMEOUT);
if(!defined('PDO_FETCH_ASSOC')) define('PDO_FETCH_ASSOC', PDO::FETCH_ASSOC);
if(!defined('PDO_FETCH_NUM')) define('PDO_FETCH_NUM', PDO::FETCH_NUM);
if(!defined('PDO_FETCH_BOTH')) define('PDO_FETCH_BOTH', PDO::FETCH_BOTH);
if(!defined('PDO_ATTR_PERSISTENT')) define('PDO_ATTR_PERSISTENT', PDO::ATTR_PERSISTENT);
if(!defined('PDO_ATTR_CASE')) define('PDO_ATTR_CASE', PDO::ATTR_CASE);
if(!defined('PDO_CASE_NATURAL')) define('PDO_CASE_NATURAL', PDO::CASE_NATURAL);
if(!defined('PDO_ATTR_AUTOCOMMIT')) define('PDO_ATTR_AUTOCOMMIT', PDO::ATTR_AUTOCOMMIT);
if(!defined('PDO_ATTR_ERRMODE')) define('PDO_ATTR_ERRMODE', PDO::ATTR_ERRMODE);
if(!defined('PDO_ERRMODE_EXCEPTION')) define('PDO_ERRMODE_EXCEPTION', PDO::ERRMODE_EXCEPTION);
if(!defined('PDO_ERRMODE_SILENT')) define('PDO_ERRMODE_SILENT', PDO::ERRMODE_SILENT);
class sqlite3 extends sqlite {
function sqlite3($dbPath) {
$this->dbVersion = 3;
if($dbPath == ':memory:') {
$this->readOnly = false;
} else {
$this->readOnly = !is_writable($dbPath);
}
parent::sqlite($dbPath);
if(class_exists('PDO') && $this->connect($dbPath)) {
return $this->connId;
} else {
$this->getError();
return false;
}
}
function connect($dbPath) {
try {
$user = '';
$password = '';
$arrayAttrib = array();
if($dbPath == ':memory:') $arrayAttrib[PDO_ATTR_PERSISTENT] = true;
$this->connId = new PDO('sqlite:'.$dbPath, $user, $password, $arrayAttrib);
$this->connId->setAttribute(PDO_ATTR_CASE, PDO_CASE_NATURAL);
$this->connId->query("PRAGMA count_changes=1;");
} catch (PDOException $e) {
$this->error = true;
$this->errorMessage = $e->getMessage();
return false;
}
if(DEBUG) {
$this->connId->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
} else {
$this->connId->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_SILENT);
}
return $this->connId;
}
function getError($errorCode = null) {
if(is_resource($this->resId)) {
$errorInfo = $this->resId->errorInfo();
} else {
$errorInfo = $this->connId->errorInfo();
}
if(is_array($errorInfo) && isset($errorInfo[2])) {
$this->error = true;
$this->errorMessage = $errorInfo[2];
} else {
$this->errorMessage = 'not an error';
}
return $this->errorMessage;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -