SQLite3与PDO操作类
class WEB {
private $conn ;
private $stmt ;
public function __construct() {
$this->CheckURL();
//$tmp = str_ireplace('common/clsUTF.php','',str_replace('\\','/',__FILE__)).'data/tt.db';
//$this->conn = $this->ConnectDB($tmp);
//exit($tmp);
//$tmp = NULL;
}
public function __destruct(){
$this->conn=null;
$this->stmt=null;
}
public function CheckURL() {
$BadUrl = "'|and|create|delete|select|update|union|;|*|(|)";
$url = empty($_SERVER["QUERY_STRING"])?"":$_SERVER["QUERY_STRING"];
if(!empty($url)) {
$arr = explode($BadUrl,"|");
foreach($arr as $v) {
if(stripos($url,$v)>0) {
$this->CatchError("URL含有敏感字符");
exit;
}
}
}
} public function CatchError($str='') {
echo '<fieldset style="width:350px;padding: 3px;"><legend> 错误描述 </legend><br> 捕捉到错误,程序结束。 <p><font style="color:#ff0000;font-size:12px;">',$str, '</font></p><a href="http://www.hazytime.cn" target="_blank" title="迷茫时代技术支持" style="margin: 3px 0px 2px 2px;color:#000;text-decoration: underline;">迷茫时代</a></fieldset>';
exit();
}
//数据库操作区-------------------------------------
public function ConnectDB($str) {
if(!isset($str) || empty($str)) $this->CatchError("连接数据库字符串不能为空");
if(!is_file($str)) $this->CatchError("没有此文件");
$this->conn = new PDO('sqlite:'.$str) or $this->CatchError('连接数据库错误');
return $this->conn;
//print_r($this->conn);exit();
}
public function query($str1,$str2='',$str3='',$db) {
if(empty($str1)) $this->CatchError('查询数据库错误');
if(!empty($str2)) {
$str2 = $this->SQLParam($str2);
$str1 = $str1.$str2;
}
if(!empty($str3)) $str1 = $str1.$str2.$str3;
$this->conn = $this->ConnectDB($db);
$result = $this->conn->query($str1) or $this->CatchError("查询出错PDO") ;
$result->setFetchMode(PDO::FETCH_ASSOC);
return $result;
}
public function querySingle($str1,$str2=1,$db) {
if(empty($str1) or empty($db)) $this->CatchError('SQL is empty!');
$this->conn = $this->ConnectDB($db);
$result = $this->conn->query($str1);//
//print_r($this->conn);exit;
if($str2==1) {
$row = $result->fetch(PDO::FETCH_ASSOC);
return $row;
}else{
//print_r($result);exit();
$row = $result->fetch(PDO::FETCH_NUM);
//$this->conn->setFetchMode(PDO::FETCH_ASSOC);
//$row = $this->conn->fetchAll();
//$row = (array)$result;
return $row[0];
}
}
public function exec($str1,$db) {
if(empty($str1)) $this->CatchError('SQL is empty!');
$this->conn = $this->ConnectDB($db);
$this->conn->exec($str1);
}
public function prepare($sql,$db) {
if(empty($sql)) $this->CatchError('prepare need SQL!');
$this->conn = $this->ConnectDB($db);
$this->stmt = $this->conn->prepare($sql);
}
public function bindValue($name,$value,$type) {
if($type == 'TEXT') {
$this->stmt->bindValue($name,$value,PDO::PARAM_STR);
}else if($type == 'INT') {
$this->stmt->bindValue($name,$value,PDO::PARAM_INT);
}
}
public function execute() {
$this->stmt->execute();
$this->stmt->closeCursor();
}
public function errorInfo() {
$arr = $this->stmt->errorInfo();
$this->CatchError($arr[2]);
$arr = NULL;
}
public function fetchColumn($int) {
if(!is_numeric($int)) $this->CatchError('fetchColumn must be a number');
$int = abs(intval($int));
return $this->stmt->fetchColumn($int) or $this->CatchError('the number is not in fetchColumn!');
}
public function SQLParam($str) {
if(is_null($str) || empty($str) || (!isset($str))) return '';
$str = str_replace("'","",$str);
$str = str_replace("\\","",$str);
$str = str_replace("\"","",$str);
$str = str_replace("&","",$str);
$str = str_replace("#","",$str);
$str = str_replace(";","",$str);
$str = str_replace("%","",$str);
$str = str_replace("`","",$str);
$str = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml);/", "$1", $str);
$str = preg_replace("/&(.)(uml);/", "$1e", $str); return $str;
}
//数据库操作区结束--------------------------------------------------
}
至此完成了操作SQLite3的PDO的类。当然你也可以修改其中的代码,以适合自己的网站。
迷茫时代的PHP版,使用PHP5.3+SQLite3(PDO)+apache2.2.14完成,择日再转。