簡潔的PHP操作SQLite類

來源:互聯網
上載者:User

標籤:

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類

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.