PHP實(shí)現(xiàn)的簡(jiǎn)單操作SQLite數(shù)據(jù)庫(kù)類與用法示例
本文實(shí)例講述了PHP實(shí)現(xiàn)的簡(jiǎn)單操作SQLite數(shù)據(jù)庫(kù)類與用法。分享給大家供大家參考,具體如下:
SQLite是一款輕型的數(shù)據(jù)庫(kù),是遵守ACID的關(guān)聯(lián)式數(shù)據(jù)庫(kù)管理系統(tǒng),它的設(shè)計(jì)目標(biāo)是嵌入式的,而且目前已經(jīng)在很多嵌入式產(chǎn)品中使用了它,它占用資源非常的低,在嵌入式設(shè)備中,可能只需要幾百K的內(nèi)存就夠了。它能夠支持Windows/Linux/Unix等等主流的操作系統(tǒng),同時(shí)能夠跟很多程序語(yǔ)言相結(jié)合,比如Tcl、PHP、Java等,還有ODBC接口,同樣比起MySQL、PostgreSQL這兩款開(kāi)源世界著名的數(shù)據(jù)庫(kù)管理系統(tǒng)來(lái)講,它的處理速度比他們都快。
這里為大家提供一個(gè)簡(jiǎn)潔的PHP操作SQLite類:
<?php /*** //應(yīng)用舉例 require_once('cls_sqlite.php'); //創(chuàng)建實(shí)例 $DB=new SQLite('blog.db'); //這個(gè)數(shù)據(jù)庫(kù)文件名字任意 //創(chuàng)建數(shù)據(jù)庫(kù)表。 $DB->query("create table test(id integer primary key,title varchar(50))"); //接下來(lái)添加數(shù)據(jù) $DB->query("insert into test(title) values('泡菜')"); $DB->query("insert into test(title) values('藍(lán)雨')"); $DB->query("insert into test(title) values('Ajan')"); $DB->query("insert into test(title) values('傲雪藍(lán)天')"); //讀取數(shù)據(jù) print_r($DB->getlist('select * from test order by id desc')); //更新數(shù)據(jù) $DB->query('update test set title = "三大" where id = 9'); ***/ 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) //直接運(yùn)行SQL,可用于更新、刪除數(shù)據(jù) { 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(); } } ?>
相關(guān) PHP 配置說(shuō)明:
1. 先測(cè)試 PHP 能否連接 sqlite 數(shù)據(jù)庫(kù):
建立一個(gè)php文件
<?php $conn = sqlite_open('test.db'); ?>
測(cè)試這個(gè)文件能否正常運(yùn)行。
如果沒(méi)有能正常加載sqlite模塊,就可能出現(xiàn)這樣的錯(cuò)誤:
Fatal error: Call to undefined function sqlite_open() in C:\Apache\Apache2\htdocs\test.php on line 2
解決辦法如下:
2. 打開(kāi) php.ini 文件,將以下三行前面的分號(hào)刪除:
;extension=php_sqlite.dll ;extension=php_pdo.dll ;extension=php_pdo_sqlite.dll
重新啟動(dòng)web服務(wù)器
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫(kù)操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP基于PDO實(shí)現(xiàn)的SQLite操作類【包含增刪改查及事務(wù)等操作】
- php使用pdo連接sqlite3的配置示例
- php封裝db類連接sqlite3數(shù)據(jù)庫(kù)的方法實(shí)例
- thinkPHP連接sqlite3數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法(附Thinkphp代碼生成器下載)
- PHP+sqlite數(shù)據(jù)庫(kù)操作示例(創(chuàng)建/打開(kāi)/插入/檢索)
- 分享php代碼將360瀏覽器導(dǎo)出的favdb的sqlite數(shù)據(jù)庫(kù)文件轉(zhuǎn)換為html
- PHP實(shí)現(xiàn)的sqlite數(shù)據(jù)庫(kù)連接類
- php讀取sqlite數(shù)據(jù)庫(kù)入門(mén)實(shí)例代碼
- PHP使用PDO操作sqlite數(shù)據(jù)庫(kù)應(yīng)用案例
相關(guān)文章
PHP管理內(nèi)存函數(shù) memory_get_usage()使用介紹
我們?cè)趯?shí)際編碼中,要想實(shí)現(xiàn)對(duì)內(nèi)存的查看和操作,許多程序員們第一個(gè)想到的就是PHP memory_get_usage()這個(gè)PHP腳本內(nèi)存函數(shù)2012-09-09php下用cookie統(tǒng)計(jì)用戶訪問(wèn)網(wǎng)頁(yè)次數(shù)的代碼
利用cookie統(tǒng)計(jì)用戶訪問(wèn)網(wǎng)頁(yè)次數(shù)的代碼,需要的朋友可以參考下。作為學(xué)習(xí)cookies的資料,不推薦使用。2010-05-05理解php Hash函數(shù),增強(qiáng)密碼安全
服務(wù)器和數(shù)據(jù)庫(kù)的資料偶爾會(huì)被竊取,因此需要保證發(fā)生這種情況時(shí)一些重要的用戶數(shù)據(jù),比如密碼,是別人無(wú)法獲取的。這里我們將要討論Hash的原理,以及它是如何保護(hù)Web應(yīng)用程序中的密碼安全的。2011-02-02php傳值和傳引用的區(qū)別點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于php傳值和傳引用的區(qū)別點(diǎn)總結(jié),需要的朋友們可以參考下。2019-11-11smarty內(nèi)置函數(shù){loteral}、{ldelim}和{rdelim}用法實(shí)例
這篇文章主要介紹了smarty內(nèi)置函數(shù){loteral}、{ldelim}和{rdelim}用法,實(shí)例分析了{(lán)loteral}、{ldelim}和{rdelim}的功能及使用技巧,需要的朋友可以參考下2015-01-01