本文實(shí)例講述了PHP使用PDO操作sqlite數(shù)據(jù)庫。分享給大家供大家參考,具體如下:
1、需求:
已知:
1)、一個(gè)json文件,里面是一個(gè)二維數(shù)組,數(shù)組解析出來為:
array (
0 =>
array (
'title' => '九十九',
),
1 =>
array (
'title' => '電腦九十九',
),
2 =>
array (
'title' => '手機(jī)九十九',
),
3 =>
array (
'title' => '手機(jī)電腦九十九',
),
);
2)、一個(gè)sqlite數(shù)據(jù)庫文件 20180824.db 新建一個(gè)sqlite數(shù)據(jù)庫文件
新建表 report
表字段 id words time
求:
把從json中查到的數(shù)據(jù),在sqlite中檢索,判斷是否存在;
如果存在就給sqlite加上一個(gè) word_sort字段,把title在文件中是第幾個(gè)(一次遞增,不是json文件數(shù)組的鍵值)寫入到word_sort字段
思路:
① 獲取jsonlist.json文件內(nèi)容并json_decode($str,true)
轉(zhuǎn)為二維數(shù)組
② 連接sqlite表
③ try{}catch(){}
給表增加 word_sort字段
④ 把json文件中的數(shù)據(jù)數(shù)組化
⑤ 每次循環(huán)5000條json數(shù)據(jù),用 IN 在report表中查詢(title字段需要拼接)
⑥ 把查詢出來的數(shù)據(jù)用 sql的批量跟新語句拼接
⑦ try{}catch(){}
批量更新report表數(shù)據(jù)
⑧ echo輸出運(yùn)行結(jié)果
2、PHP代碼(yaf框架):
?php
/**
* @todo 組詞
* Class CommunityController
*/
class CombinwordController extends Rest{
/**
* @todo 判斷.json數(shù)據(jù)是否存在,存在把數(shù)據(jù)往前排
* @linux 212 /usr/local/php7/bin/php /var/www/web/shop/public/cli.php request_uri="/v1/combinword/index"
*/
public function indexAction(){
set_time_limit ( 0 ); //設(shè)置時(shí)間不過時(shí)
$data = $this->getjson(); //獲取json數(shù)據(jù)
$dbfile_path = APP_PATH.'/data/combinword/20180824.db';
$db = new PDO("sqlite:{$dbfile_path}");
//設(shè)置數(shù)據(jù)庫句柄 屬性 PDO::ATTR_ERRMODE:錯(cuò)誤報(bào)告。 PDO::ERRMODE_EXCEPTION: 拋出 exceptions 異常。
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//加combinword字段 START
$add_filed = 'word_sort';
$add_filed_sql = "alter table report add {$add_filed} TEXT(32)";
try {
$db->beginTransaction();//啟動(dòng)事務(wù)
$db->exec($add_filed_sql); //加字段
$db->commit();//提交事務(wù)
}catch(PDOException $e){
//$e->getMessage();//獲取錯(cuò)誤信息。
echo '字段已經(jīng)存在'.PHP_EOL;
$db->rollBack();//回滾,如果一個(gè)地方出現(xiàn)錯(cuò)誤,回到總體操作之前。
}
//加combinword字段 END
$addStep = 5000; //每次操作的數(shù)據(jù)
$word_cnt = 0;
$succ_cnt = 0;
$sort = 0;
$total = count($data);
for ( $x=0; $x$total; $x += $addStep ){
$temp_json = array_slice($data, $x, $addStep); //批量操作 100條
$temp_json = array_column( $temp_json, "title" );
$temp_json = array_unique($temp_json);
$temp_str = $this->getStrByArr($temp_json);
$temp_sql = "select * from report where words IN ({$temp_str})";
$res = $db->query($temp_sql);
$result = $res->fetchAll(PDO::FETCH_ASSOC); //獲取數(shù)組結(jié)果集
$words_result = array_column($result, 'words'); //結(jié)果去重
$unique_result = array_unique($words_result);
//var_export($unique_result);die;
//批量更新 START
$update_sql = "UPDATE report SET {$add_filed} = CASE words ";
foreach ($unique_result as $k => $v){
$updateValue = $v;
$update_sql .= " WHEN '{$updateValue}' THEN ".$sort++;
}
$sort += count($unique_result); //加上排序字段
$update_sql_str = $this->getStrByArr( $unique_result );
$update_sql .= " END WHERE words IN ({$update_sql_str})";
//var_export($update_sql);die;
try {
$db->beginTransaction();//啟動(dòng)事務(wù)
$cnt = $db->exec($update_sql); //加字段
$db->commit();//提交事務(wù)
$word_cnt += count($result);
$succ_cnt += $cnt;
echo "更新了[{".count($result)."}]個(gè)關(guān)鍵字,共影響了[{$cnt}]條數(shù)據(jù) ".PHP_EOL;
}catch(PDOException $e){
//$e->getMessage();//獲取錯(cuò)誤信息。
echo "批量更新失敗 ".PHP_EOL;
$db->rollBack();//回滾,如果一個(gè)地方出現(xiàn)錯(cuò)誤,回到總體操作之前。
}
//批量更新END
}
echo "一共更新了[{$word_cnt}]個(gè)關(guān)鍵字,共影響了[{$succ_cnt}]條數(shù)據(jù) ".PHP_EOL;
die;
}
/**
* @todo 根據(jù)數(shù)組返回拼接的字符串
* @param unknown $temp_json 數(shù)組
* @return string 字符串
*/
function getStrByArr($temp_json){
$temp_str = '';
$count = count($temp_json);
$lastValue = end($temp_json);//var_export($lastValue);die; //獲取數(shù)組最后一個(gè)元素
foreach ($temp_json as $k => $v){
$next_str = '';
if($v != $lastValue ){ //不是最后一個(gè)
$next_str = ',';
}else{
$next_str = '';
}
$temp_str .= "'".$v."'{$next_str}";
}
return $temp_str;
}
/**
* @todo 獲取json數(shù)據(jù)
*/
public function getjson(){
$filename = APP_PATH.'/data/combinword/jsonlist.json';
$json = file_get_contents($filename);
$array = json_decode($json, true);
return $array;
}
}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- PHP使用PDO實(shí)現(xiàn)mysql防注入功能詳解
- PHP連接MySQL數(shù)據(jù)庫的三種方式實(shí)例分析【mysql、mysqli、pdo】
- PHP使用PDO、mysqli擴(kuò)展實(shí)現(xiàn)與數(shù)據(jù)庫交互操作詳解
- PHP使用PDO創(chuàng)建MySQL數(shù)據(jù)庫、表及插入多條數(shù)據(jù)操作示例
- php使用mysqli和pdo擴(kuò)展,測試對比mysql數(shù)據(jù)庫的執(zhí)行效率完整示例
- php使用mysqli和pdo擴(kuò)展,測試對比連接mysql數(shù)據(jù)庫的效率完整示例
- PHP實(shí)現(xiàn)PDO操作mysql存儲(chǔ)過程示例
- PHP基于PDO擴(kuò)展操作mysql數(shù)據(jù)庫示例
- PHP基于pdo的數(shù)據(jù)庫操作類【可支持mysql、sqlserver及oracle】
- PHP如何初始化PDO及原始SQL語句操作