標籤:
SQLite是一款輕型的資料庫,是遵守ACID的關聯式資料庫管理系統,它的設計目標是嵌入式的,而且目前已經在很多嵌入式產品中使用了它,它佔用資源非常的低,在嵌入式裝置中,可能只需要幾百K的記憶體就夠了。它能夠支援Windows/Linux/Unix等等主流的作業系統,同時能夠跟很多程式語言相結合,比如Tcl、PHP、Java等,還有ODBC介面,同樣比起Mysql、PostgreSQL這兩款開源世界著名的資料庫管理系統來講,它的處理速度比他們都快。
這裡為大家提供一個簡潔的PHP操作SQLite類:
<?php/***//應用舉例require_once(‘cls_sqlite.php‘);//建立執行個體$DB=new SQLite(‘blog.db‘); //這個資料庫檔案名字任意//建立資料庫表。$DB->query("create table test(id integer primary key,title varchar(50))");//接下來添加資料$DB->query("insert into test(title) values(‘泡菜‘)");$DB->query("insert into test(title) values(‘藍雨‘)");$DB->query("insert into test(title) values(‘Ajan‘)");$DB->query("insert into test(title) values(‘傲雪藍天‘)");//讀取資料print_r($DB->getlist(‘select * from test order by id desc‘));//更新資料$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) //直接運行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();}}?>
相關 PHP 配置說明:
1. 先測試 PHP 能否串連 sqlite 資料庫:
建立一個php檔案
<?php$conn = sqlite_open(‘test.db‘);?>
測試這個檔案能否正常運行。
如果沒有能正常載入sqlite模組,就可能出現這樣的錯誤:
Fatal error: Call to undefined function sqlite_open() in C:\Apache\Apache2\htdocs\test.php on line 2
解決辦法如下:
2. 開啟 php.ini 檔案,將以下三行前面的分號刪除:
;extension=php_sqlite.dll
;extension=php_pdo.dll
;extension=php_pdo_sqlite.dll
重新啟動web伺服器
http://blog.csdn.net/diandian_520/article/details/7231855
簡潔的PHP操作SQLite類