这个类既可以在SQLite3下直接操作也可以通过PDO操作SQLite3,但不会封装数据库操作。现贴出代码,希望能得到高手的帮助。这个类有很多的错误之处,如果你要调试真实数据的话,请到这里下载:http://www.uushare.com/user/littlenew/file/2840401class WEB {
private $IS_PDO = 1;//是否使用PDO操作sqlite3数据库
private $conn = NULL;
public function __construct() {
$this->CheckURL();
}
public function __destruct(){
$this->conn=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='',$str1=1) {
if(!isset($str) || empty($str) || empty($str1)) $this->CatchError("连接数据库字符串不能为空");
if(!is_file($str)) $this->CatchError("没有此文件");
if($this->IS_PDO == 1) {
$this->conn = new PDO('sqlite:'.$str) or $this->CatchError('连接数据库错误');
}else{
if($str1==1)
$this->conn = new SQLITE3($str,SQLITE3_OPEN_READONLY) or $this->CatchError("连接数据库错误READONLY");
else if($str1==2)
$this->conn = new SQLITE3($str,SQLITE3_OPEN_READWRITE) or $this->CatchError("连接数据库错误READWRITE");
else if($str1==3)
$this->conn = new SQLITE3($str,SQLITE3_OPEN_CREATE) or $this->CatchError("连接数据库错误CREATE");
}
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);
if($this->IS_PDO == 1) {
$result = $this->conn->query($str1) or $this->CatchError("查询出错PDO") ;
$result->setFetchMode(PDO::FETCH_ASSOC);
$row = $result;
}else{
$result = $this->conn->query($str1) or $this->CatchError("查询出错");
$row = $result->fetchArray(SQLITE3_ASSOC);
}
return $row;
}
public function querySingle($str1,$str2=1,$db) {
if(empty($str1) or empty($db)) $this->CatchError('SQL is empty!');
$this->conn = $this->ConnectDB($db);
if($this->IS_PDO == 1) {
$result = $this->conn->query($str1);//
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];
}
}else{
if($str2)
$row = $this->conn->querySingle($str1,true) or $this->CatchError("querySingle!SQLite3");
else
$row = $this->conn->querySingle($str1,false) or $this->CatchError("SQLite3");
return $row;
}
}
public function exec($str1,$db,$str2=1) {
if(empty($str1)) $this->CatchError('SQL is empty!');
$this->conn = $this->ConnectDB($db,$str2);
$this->conn->exec($str1);
}
public function prepare($sql,$db) {
if(empty($sql)) $this->CatchError('prepare need SQL!');
$this->conn = $this->ConnectDB($db,2);
$this->conn->prepare($sql);
}
public function bindValue($name,$value,$type) {
if($this->IS_PDO == 1) {
if($type == 'TEXT') {
$this->conn->bindValue($name,$value,PDO::PARAM_STR);
}else if($type == 'INT') {
$this->conn->bindValue($name,$value,PDO::PARAM_INT);
}
}else{
if($type == 'TEXT') {
$this->conn->bindValue($name,$value,SQLITE3_TEXT);
}else if($type == 'INT') {
$this->conn->bindValue($name,$value,SQLITE3_INTEGER);
}
}
}
public function execute() {
if($this->IS_PDO == 1) {
$this->conn->execute();
}else{
$this->conn->execute();
$this->conn->close();
}
}
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;
}
}我期望得到的使用方法: $sys = new WEB;
$sys->prepare($sql,secondDB);
$sys->bindValue(':ID',$id,'INT');
$sys->bindValue(':replay',$replay,'TEXT');
$sys->execute();sqlite3的操作不用管,高手能否将带颜色的部分修正一下。让它能正常工作。说白了,就是封装PDO操作sqlite3的数据库操作部分。
private $IS_PDO = 1;//是否使用PDO操作sqlite3数据库
private $conn = NULL;
public function __construct() {
$this->CheckURL();
}
public function __destruct(){
$this->conn=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='',$str1=1) {
if(!isset($str) || empty($str) || empty($str1)) $this->CatchError("连接数据库字符串不能为空");
if(!is_file($str)) $this->CatchError("没有此文件");
if($this->IS_PDO == 1) {
$this->conn = new PDO('sqlite:'.$str) or $this->CatchError('连接数据库错误');
}else{
if($str1==1)
$this->conn = new SQLITE3($str,SQLITE3_OPEN_READONLY) or $this->CatchError("连接数据库错误READONLY");
else if($str1==2)
$this->conn = new SQLITE3($str,SQLITE3_OPEN_READWRITE) or $this->CatchError("连接数据库错误READWRITE");
else if($str1==3)
$this->conn = new SQLITE3($str,SQLITE3_OPEN_CREATE) or $this->CatchError("连接数据库错误CREATE");
}
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);
if($this->IS_PDO == 1) {
$result = $this->conn->query($str1) or $this->CatchError("查询出错PDO") ;
$result->setFetchMode(PDO::FETCH_ASSOC);
$row = $result;
}else{
$result = $this->conn->query($str1) or $this->CatchError("查询出错");
$row = $result->fetchArray(SQLITE3_ASSOC);
}
return $row;
}
public function querySingle($str1,$str2=1,$db) {
if(empty($str1) or empty($db)) $this->CatchError('SQL is empty!');
$this->conn = $this->ConnectDB($db);
if($this->IS_PDO == 1) {
$result = $this->conn->query($str1);//
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];
}
}else{
if($str2)
$row = $this->conn->querySingle($str1,true) or $this->CatchError("querySingle!SQLite3");
else
$row = $this->conn->querySingle($str1,false) or $this->CatchError("SQLite3");
return $row;
}
}
public function exec($str1,$db,$str2=1) {
if(empty($str1)) $this->CatchError('SQL is empty!');
$this->conn = $this->ConnectDB($db,$str2);
$this->conn->exec($str1);
}
public function prepare($sql,$db) {
if(empty($sql)) $this->CatchError('prepare need SQL!');
$this->conn = $this->ConnectDB($db,2);
$this->conn->prepare($sql);
}
public function bindValue($name,$value,$type) {
if($this->IS_PDO == 1) {
if($type == 'TEXT') {
$this->conn->bindValue($name,$value,PDO::PARAM_STR);
}else if($type == 'INT') {
$this->conn->bindValue($name,$value,PDO::PARAM_INT);
}
}else{
if($type == 'TEXT') {
$this->conn->bindValue($name,$value,SQLITE3_TEXT);
}else if($type == 'INT') {
$this->conn->bindValue($name,$value,SQLITE3_INTEGER);
}
}
}
public function execute() {
if($this->IS_PDO == 1) {
$this->conn->execute();
}else{
$this->conn->execute();
$this->conn->close();
}
}
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;
}
}我期望得到的使用方法: $sys = new WEB;
$sys->prepare($sql,secondDB);
$sys->bindValue(':ID',$id,'INT');
$sys->bindValue(':replay',$replay,'TEXT');
$sys->execute();sqlite3的操作不用管,高手能否将带颜色的部分修正一下。让它能正常工作。说白了,就是封装PDO操作sqlite3的数据库操作部分。
paepare的返回值你没有接收? 后面的execute应该是在那个返回的statement上操作的
$sys->prepare($sql,secondDB);
$sys->bindValue(':ID',$id,'INT');
$sys->bindValue(':replay',$replay,'TEXT');
$sys->execute();
毕竟PDO的一些操作数据库的东西是和PDO相关的,也就是说要在类的内部返回一个PDO对象。这个我就不会了。
比如,
$st1=$dbh->prepare($sql1);
...其它语句...
$st2=$dbh->prepare($sql2);
...其它语句...
$st1->bindValue(':xx',$value1,PDO::PARAM_INT);
...其它语句...
$st2->bindValue(':yy',$value2,PDO::PARAM_INT);
...其它语句...
$st1->execute();
...其它语句...
$st2->execute();
...其它语句...
但你这种方式,只能依次运行.....因为都在$sys上操作,
除非你再加参数....
或者放弃这种交错运行的方式这个你如何取舍
都这么晚了,还来回贴,谢谢!!我暂时放弃这种交错运行的方式,所有的数据库操作都在$sys上进行。麻烦您了。
如果在PDO模式下, 用它来保存PDO::prepare产生的statement对象,
后面的execute实际上就在这个statement属性上操作
这样就可以实现PDO的方式你可以自己先试试,有问题再来讨论还有,我觉得你这种模式,最好将这个分成3个类(或2个)来实现一个抽象的SQLite3DB类,定义统一的接口prepare,execute等然后一个SQLite3继承SQLite3DB实现普通方式的接口
另一个PDOSQLite3继承SQLite3DB实现PDO方式的接口
注意它们两的接口(方法名及参数)是必须是完全一样的(所以我们要用一个抽象类来控制)
这样写,每个类里就单纯考虑一种方式下的数据库操作,比较简单清楚,
你的代码里也就不必有那么多的if($this->IS_PDO == 1) { 等,
我上面说的statement也只需要放在PDOSQLite3里,你可以尝试一下
if(empty($sql)) $this->CatchError('prepare need SQL!');
$this->conn = $this->ConnectDB($db,2);
$this->statement = $this->conn->prepare($sql);
}
public function bindValue($name,$value,$type) {
if($this->IS_PDO == 1) {
if($type == 'TEXT') {
$this->statement->bindValue($name,$value,PDO::PARAM_STR);
}else if($type == 'INT') {
$this->statement->bindValue($name,$value,PDO::PARAM_INT);
......
@@xuzuning
这里贴的函数没有更新,实际上我使用的函数已经更新了。函数是正确的。请问:如果我在__construct里调用连接数据库,是不是在整个类里都可以使用$this->conn去执行其它的动作????
mysql_pconnect — 打开一个到 MySQL 服务器的持久连接 说明
resource mysql_pconnect ([ string $server [, string $username [, string $password [, int $client_flags ]]]] )
忘了...你这个不是MYSQL的...
对,我这个数据库不是mysql。希望楼下能帮我解答:如果我在__construct里调用连接数据库,是不是在整个类里都可以使用$this->conn去执行其它的动作????
ORM类。使用pdo方式
http://code.google.com/p/queryphp/downloads/list