php连接sqlite
<?php
header("Content-type:text/html;charset=utf-8");
set_time_limit(0);
class SQLite
{
function __construct($file)
{
try {
$this->connection = new PDO('sqlite:' . $file);
} catch (PDOException $e) {
try {
$this->connection = new PDO('sqlite2:' . $file);
} catch (PDOException $e) {
exit('error!');
}
}
}
function __destruct()
{
$this->connection = null;
}
function query($sql) //直接运行SQL,可用于更新、删除数据
{
return $this->connection->query($sql);
}
function getlist($sql) //取得记录列表
{
$recordlist = array();
foreach ($this->query($sql) as $rstmp) {
$recordlist[] = $rstmp;
}
return $recordlist;
}
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();
}
}
function grabImage($url,$fileName){
if($url=="") return false;
$p=strrpos($url,".");
$exp=substr($url,$p+1);
if(strpos(strtolower($exp), 'jpg') !== false){
$exp = 'jpg';
}
if(strpos(strtolower($exp), 'gif') !== false){
$exp = 'gif';
}
if(strpos(strtolower($exp), 'jpeg') !== false){
$exp = 'jpeg';
}
if(strpos(strtolower($exp), 'png') !== false){
$exp = 'png';
}
$fileName=$fileName.".".$exp;
if($exp=="gif" || $exp=="jpeg" || $exp=="png" || $exp=="jpg"){
ob_start();
readfile($url);
$content=ob_get_contents();
ob_end_clean();
$f=fopen($fileName,"w");
fwrite($f,$content);
fclose($f);
}else{
return false;
}
return $fileName;
}
//连接sqllite
$DB = new SQLite('news.db3');
$page = intval($_GET['page']);if(empty($page)) $page=1;
$nextpage = $page+1;
$pagesize = 10;
$nums = $DB->RecordCount("select id from Content order by id desc");
$pages = ceil($nums/$pagesize);
$firstR = ($page-1)*$pagesize;
$r = $DB->getlist("select * from Content order by id desc limit $firstR,$pagesize");
//连接数据库
$con2 = mysqli_connect("localhost", "root", "root", "magiccore");
if (!$con2) {
die('Could not connect: ' . mysqli_error());
}
mysqli_query($con2, 'SET NAMES UTF8');
foreach ($r as $v) {
$title = addslashes($v['bt']);
$content = addslashes($v['nr']);
$add_time = floatval(strtotime(trim($v['rq'])));
$tp = str_replace(['!1'],['!3'],$v['tp']);
$newname = 'uploads/allimg/news/'.date('YmdHis'.rand(100000,999999));
$newurl = '/'.grabImage($tp, $newname);
$litpic = $newurl;
$sql = "insert into magiccore_archives(typeid,title,channel,is_litpic,litpic,click,arcrank,seo_title,seo_keywords,seo_description,sort_order,lang,add_time,update_time) values(18, '".$title."', 1, 1, '$litpic',0,0,'$title','$title','$title','1000','cn','$add_time','$add_time');";
$r = mysqli_query($con2, $sql);
if (!$r) {
die($sql);
}
$id = mysqli_insert_id($con2);
$ljj = $v['jj'];
$sql = "insert into magiccore_article_content(aid,content,ljj,add_time,update_time) values($id,'$content', '$ljj','$add_time','$add_time')";
mysqli_query($con2, $sql);
//exit();
}
if($nextpage <= $pages){
$url = '?page='.$nextpage;
echo '<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body><script>window.setTimeout(function(){window.location.href="'.$url.'"}, 1000)</script></body></html>';
}else{
exit('已完成');
}
?>