今天一个朋友想使用PHP读取火车采集器采集来的数据库,因为他使用的是标准版,火车采集器标准版默认使用的是SQLITE数据库,想起原来在旧的博客里面有一个PHP操作SQLITE数据库的类,便找出来,继续分享,很简洁的说。
<?php
/**
* 使用方法
* $DB=new SQLite('SpiderResult.db3');
* $ARR = $DB->RecordCount('select * from Content order by ID desc');
* print_r($ARR);
*/
class SQLite {
function __construct($file) {
try {
$this -> Connection = new PDO('sqlite2:' . $file);
}
catch(PDOException $e) {
try {
$this -> Connection = new PDO('sqlite:' . $file);
}
catch(PDOException $e) {
exit('error!');
}
}
}
function __destruct() {
$this -> Connection = null;
}
function Query($SQL) {
return $this -> Connection -> Query($SQL);
}
function Execute($SQL) {
return $this -> Query($SQL) -> fetch();
}
function RecordArray($SQL) {
return $this -> Query($SQL) -> fetchAll();
}
function RecordCount($SQL) {
return count($this -> RecordArray($SQL));
}
function RecordLastID() {
return $this -> Connection -> lastInsertId();
}
}
?>