因为需要为一批不大不小的文件插入到数据库分析,为操作方便所以选择了php和Sqlite3。刚开始直接使用的SQLite3类插入数据,我的个天呀慢的要死,30秒只插入了300多条数据最后还提示php-cgi.exe超时了。后来查资料得知用事务提交的方式会比较快,经测试成功后写了一个php类贴出来,用MySQL数据库也可用类似的方式。
class MyDB extends PDO
{
protected $transactionCounter = 0;
protected $lasterror = "";
function __construct($index = 0)
{
try{
if($index == 1){
parent::__construct('sqlite:'.dirname(__FILE__)."/data.db");
}
else{
parent::__construct('sqlite:'.dirname(__FILE__)."/tools.db");
}
$this->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
$this->lasterror = $e->getMessage();
}
}
function query($sql)
{
$result = array();
try{
$sth = $this->prepare($sql);
if(!$sth){
$this->lasterror = "Incorrect statement";
return array();
}
$sth->execute