SQLite PHP
SQLite3 擴展在PHP 5.3.0是默認啟用。這也可能它通過使用禁用 --without-sqlite3 在編譯的時候。
Windows用戶必須啟用php_sqlite3.dll為了使用這個擴展。這個DLL包含與Windows分派PHP 如 PHP 5.3.0。
有關詳細的安裝說明,請查看我們的PHP教學和其官方網站上。
PHP 接口API
以下是重要的PHP程序,就足夠完成工作要求從PHP程序使用SQLite數據庫。如果要找一個更複雜的應用程序,那麼你可以看看到PHP官方文件。
S.N. | API & 描述 |
---|---|
1 |
public void SQLite3::open ( filename, flags, encryption_key ) 打開一個SQLite3數據庫。如果構建包括加密,然後它會試圖使用密鑰。 If the filename is given as ':memory:', SQLite3::open() will create an in-memory database in RAM that lasts only for the duration of the session. If filename is actual device file name, SQLite3::open() attempts to open the database file by using its value. If no file by that name exists then a new database file by that name gets created. Optional flags used to determine how to open the SQLite database. By default, open uses SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE. |
2 |
public bool SQLite3::exec ( string $query ) This routine provides a quick, easy way to execute SQL commands provided by sql argument which can consist of more than one SQL command. This routine is used to execute a result-less query against a given database. |
3 |
public SQLite3Result SQLite3::query ( string $query ) This routine executes an SQL query, returning an SQLite3Result object if the query returns results. |
4 |
public int SQLite3::lastErrorCode ( void ) This routine returns the numeric result code of the most recent failed SQLite request |
5 |
public string SQLite3::lastErrorMsg ( void ) This routine returns english text describing the most recent failed SQLite request. |
6 |
public int SQLite3::changes ( void ) This routine returns the number of database rows that were updated or inserted or deleted by the most recent SQL statement |
7 |
public bool SQLite3::close ( void ) This routine closes a database connection previously opened by a call to SQLite3::open(). |
8 |
public string SQLite3::escapeString ( string $value ) This routine returns a string that has been properly escaped for safe inclusion in an SQL statement. |
連接到數據庫
下麵的PHP代碼顯示了如何連接到一個現有的數據庫。如果數據庫不存在,那麼它就會被創建,終於將返回一個數據庫對象。
<?php class MyDB extends SQLite3 { function __construct() { $this->open('test.db'); } } $db = new MyDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Opened database successfully "; } ?>
現在,讓我們運行上麵的程序在當前目錄中創建我們的數據庫test.db。按要求可以改變你的路徑。如果數據庫創建成功,則它會給下麵的消息:
Open database successfully
創建表
下麵的PHP程序將使用以前創建的數據庫中創建一個表:
<?php class MyDB extends SQLite3 { function __construct() { $this->open('test.db'); } } $db = new MyDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Opened database successfully "; } $sql =<<<EOF CREATE TABLE COMPANY (ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL); EOF; $ret = $db->exec($sql); if(!$ret){ echo $db->lastErrorMsg(); } else { echo "Table created successfully "; } $db->close(); ?>
上述程序執行時,它會創建表COMPANYtest.db的,它會顯示以下消息:
Opened database successfully Table created successfully
INSERT 操作
PHP程序,顯示我們如何能夠創造我們的COMPANY在上麵的例子中創建表中的記錄:
<?php class MyDB extends SQLite3 { function __construct() { $this->open('test.db'); } } $db = new MyDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Opened database successfully "; } $sql =<<<EOF INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Paul', 32, 'California', 20000.00 ); INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Teddy', 23, 'Norway', 20000.00 ); INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 ); EOF; $ret = $db->exec($sql); if(!$ret){ echo $db->lastErrorMsg(); } else { echo "Records created successfully "; } $db->close(); ?>
上述程序執行時,它會創建COMPANY表中的記錄,並顯示以下兩行:
Opened database successfully Records created successfully
SELECT 操作
PHP程序表明我們如何能夠獲取並顯示COMPANY 表在上麵的例子中創建表的記錄:
<?php class MyDB extends SQLite3 { function __construct() { $this->open('test.db'); } } $db = new MyDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Opened database successfully "; } $sql =<<<EOF SELECT * from COMPANY; EOF; $ret = $db->query($sql); while($row = $ret->fetchArray(SQLITE3_ASSOC) ){ echo "ID = ". $row['ID'] . " "; echo "NAME = ". $row['NAME'] ." "; echo "ADDRESS = ". $row['ADDRESS'] ." "; echo "SALARY = ".$row['SALARY'] ." "; } echo "Operation done successfully "; $db->close(); ?>
當上述程序執行時,它會產生以下結果:
Opened database successfully ID = 1 NAME = Paul ADDRESS = California SALARY = 20000 ID = 2 NAME = Allen ADDRESS = Texas SALARY = 15000 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000 Operation done successfully
UPDATE 操作
下麵的PHP代碼顯示了如何我們可以使用UPDATE語句來更新任何記錄,然後獲取並顯示更新的記錄,從COMPANY 表:
<?php class MyDB extends SQLite3 { function __construct() { $this->open('test.db'); } } $db = new MyDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Opened database successfully "; } $sql =<<<EOF UPDATE COMPANY set SALARY = 25000.00 where ID=1; EOF; $ret = $db->exec($sql); if(!$ret){ echo $db->lastErrorMsg(); } else { echo $db->changes(), " Record updated successfully "; } $sql =<<<EOF SELECT * from COMPANY; EOF; $ret = $db->query($sql); while($row = $ret->fetchArray(SQLITE3_ASSOC) ){ echo "ID = ". $row['ID'] . " "; echo "NAME = ". $row['NAME'] ." "; echo "ADDRESS = ". $row['ADDRESS'] ." "; echo "SALARY = ".$row['SALARY'] ." "; } echo "Operation done successfully "; $db->close(); ?>
當上述程序執行時,它會產生以下結果:
Opened database successfully 1 Record updated successfully ID = 1 NAME = Paul ADDRESS = California SALARY = 25000 ID = 2 NAME = Allen ADDRESS = Texas SALARY = 15000 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000 Operation done successfully
DELETE 操作
下麵的PHP代碼,表明我們如何能夠使用DELETE語句刪除任何記錄,然後獲取並顯示剩餘的記錄COMPANY 表:
<?php class MyDB extends SQLite3 { function __construct() { $this->open('test.db'); } } $db = new MyDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Opened database successfully "; } $sql =<<<EOF DELETE from COMPANY where ID=2; EOF; $ret = $db->exec($sql); if(!$ret){ echo $db->lastErrorMsg(); } else { echo $db->changes(), " Record deleted successfully "; } $sql =<<<EOF SELECT * from COMPANY; EOF; $ret = $db->query($sql); while($row = $ret->fetchArray(SQLITE3_ASSOC) ){ echo "ID = ". $row['ID'] . " "; echo "NAME = ". $row['NAME'] ." "; echo "ADDRESS = ". $row['ADDRESS'] ." "; echo "SALARY = ".$row['SALARY'] ." "; } echo "Operation done successfully "; $db->close(); ?>
當上述程序執行時,它會產生以下結果:
Opened database successfully 1 Record deleted successfully ID = 1 NAME = Paul ADDRESS = California SALARY = 25000 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000 Operation done successfully