PHP使用PDO操作sqlite數(shù)據(jù)庫應(yīng)用案例
本文實(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ù)庫操作技巧匯總》
希望本文所述對(duì)大家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ò)展,測(cè)試對(duì)比mysql數(shù)據(jù)庫的執(zhí)行效率完整示例
- php使用mysqli和pdo擴(kuò)展,測(cè)試對(duì)比連接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語句操作
相關(guān)文章
php版微信支付api.mch.weixin.qq.com域名解析慢原因與解決方法
這篇文章主要介紹了php版微信支付api.mch.weixin.qq.com域名解析慢原因與解決方法,詳細(xì)分析了微信支付api.mch.weixin.qq.com域名解析慢原因與使用curl_easy_setopt指定ipv4解決ipv6解析問題的相關(guān)技巧,需要的朋友可以參考下2016-10-10PHP中實(shí)現(xiàn)接收多個(gè)name相同但Value不相同表單數(shù)據(jù)實(shí)例
這篇文章主要介紹了PHP中實(shí)現(xiàn)接收多個(gè)name相同但Value不相同表單數(shù)據(jù)實(shí)例,需要的朋友可以參考下2015-02-02PHP基于反射機(jī)制實(shí)現(xiàn)自動(dòng)依賴注入的方法詳解
這篇文章主要介紹了PHP基于反射機(jī)制實(shí)現(xiàn)自動(dòng)依賴注入的方法,結(jié)合實(shí)例形式分析了php使用反射實(shí)現(xiàn)自動(dòng)依賴注入的步驟、原理與相關(guān)操作技巧,需要的朋友可以參考下2017-12-12php設(shè)計(jì)模式 Strategy(策略模式)
定義一系列算法,把它們一個(gè)個(gè)封裝起來,并且使它們可相互替換,使用得算法的變化可獨(dú)立于使用它的客戶2011-06-06PHP配合fiddler抓包抓取微信指數(shù)小程序數(shù)據(jù)的實(shí)現(xiàn)方法分析
這篇文章主要介紹了PHP配合fiddler抓包抓取微信指數(shù)小程序數(shù)據(jù)的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了PHP結(jié)合fiddler抓取微信指數(shù)小程序數(shù)據(jù)的相關(guān)原理與實(shí)現(xiàn)方法,需要的朋友可以參考下2020-01-01