TShopping

 找回密碼
 註冊
搜索
查看: 53|回復: 0

[教學] PHP 下使用 SQLite

[複製鏈接]
發表於 2019-9-12 21:37:50 | 顯示全部樓層 |閱讀模式
 
Push to Facebook Push to Plurk  
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代碼顯示了如何連接到一個現有的數據庫。如果數據庫不存在,那麼它就會被創建,終於將返回一個數據庫對象。

  1. <?php
  2.    class MyDB extends SQLite3
  3.    {
  4.       function __construct()
  5.       {
  6.          $this->open('test.db');
  7.       }
  8.    }
  9.    $db = new MyDB();
  10.    if(!$db){
  11.       echo $db->lastErrorMsg();
  12.    } else {
  13.       echo "Opened database successfully
  14. ";
  15.    }
  16. ?>
複製代碼

現在,讓我們運行上麵的程序在當前目錄中創建我們的數據庫test.db。按要求可以改變你的路徑。如果數據庫創建成功,則它會給下麵的消息:

Open database successfully
創建表
下面的PHP程序將使用以前創建的數據庫中創建一個表:

  1. <?php
  2.    class MyDB extends SQLite3
  3.    {
  4.       function __construct()
  5.       {
  6.          $this->open('test.db');
  7.       }
  8.    }
  9.    $db = new MyDB();
  10.    if(!$db){
  11.       echo $db->lastErrorMsg();
  12.    } else {
  13.       echo "Opened database successfully
  14. ";
  15.    }

  16.    $sql =<<<EOF
  17.       CREATE TABLE COMPANY
  18.       (ID INT PRIMARY KEY     NOT NULL,
  19.       NAME           TEXT    NOT NULL,
  20.       AGE            INT     NOT NULL,
  21.       ADDRESS        CHAR(50),
  22.       SALARY         REAL);
  23. EOF;

  24.    $ret = $db->exec($sql);
  25.    if(!$ret){
  26.       echo $db->lastErrorMsg();
  27.    } else {
  28.       echo "Table created successfully
  29. ";
  30.    }
  31.    $db->close();
  32. ?>
複製代碼

上述程序執行時,它會創建表COMPANYtest.db的,它會顯示以下消息:

Opened database successfully
Table created successfully
INSERT 操作
PHP程序,顯示我們如何能夠創造我們的COMPANY在上麵的例子中創建表中的記錄:

  1. <?php
  2.    class MyDB extends SQLite3
  3.    {
  4.       function __construct()
  5.       {
  6.          $this->open('test.db');
  7.       }
  8.    }
  9.    $db = new MyDB();
  10.    if(!$db){
  11.       echo $db->lastErrorMsg();
  12.    } else {
  13.       echo "Opened database successfully
  14. ";
  15.    }

  16.    $sql =<<<EOF
  17.       INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
  18.       VALUES (1, 'Paul', 32, 'California', 20000.00 );

  19.       INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
  20.       VALUES (2, 'Allen', 25, 'Texas', 15000.00 );

  21.       INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
  22.       VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );

  23.       INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
  24.       VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );
  25. EOF;

  26.    $ret = $db->exec($sql);
  27.    if(!$ret){
  28.       echo $db->lastErrorMsg();
  29.    } else {
  30.       echo "Records created successfully
  31. ";
  32.    }
  33.    $db->close();
  34. ?>
複製代碼

上述程序執行時,它會創建COMPANY表中的記錄,並顯示以下兩行:

Opened database successfully
Records created successfully
SELECT 操作
PHP程序表明我們如何能夠獲取並顯示COMPANY 表在上麵的例子中創建表的記錄:

  1. <?php
  2.    class MyDB extends SQLite3
  3.    {
  4.       function __construct()
  5.       {
  6.          $this->open('test.db');
  7.       }
  8.    }
  9.    $db = new MyDB();
  10.    if(!$db){
  11.       echo $db->lastErrorMsg();
  12.    } else {
  13.       echo "Opened database successfully
  14. ";
  15.    }

  16.    $sql =<<<EOF
  17.       SELECT * from COMPANY;
  18. EOF;

  19.    $ret = $db->query($sql);
  20.    while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
  21.       echo "ID = ". $row['ID'] . "
  22. ";
  23.       echo "NAME = ". $row['NAME'] ."
  24. ";
  25.       echo "ADDRESS = ". $row['ADDRESS'] ."
  26. ";
  27.       echo "SALARY =  ".$row['SALARY'] ."

  28. ";
  29.    }
  30.    echo "Operation done successfully
  31. ";
  32.    $db->close();
  33. ?>
複製代碼

當上述程序執行時,它會產生以下結果:

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 表:

  1. <?php
  2.    class MyDB extends SQLite3
  3.    {
  4.       function __construct()
  5.       {
  6.          $this->open('test.db');
  7.       }
  8.    }
  9.    $db = new MyDB();
  10.    if(!$db){
  11.       echo $db->lastErrorMsg();
  12.    } else {
  13.       echo "Opened database successfully
  14. ";
  15.    }
  16.    $sql =<<<EOF
  17.       UPDATE COMPANY set SALARY = 25000.00 where ID=1;
  18. EOF;
  19.    $ret = $db->exec($sql);
  20.    if(!$ret){
  21.       echo $db->lastErrorMsg();
  22.    } else {
  23.       echo $db->changes(), " Record updated successfully
  24. ";
  25.    }

  26.    $sql =<<<EOF
  27.       SELECT * from COMPANY;
  28. EOF;
  29.    $ret = $db->query($sql);
  30.    while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
  31.       echo "ID = ". $row['ID'] . "
  32. ";
  33.       echo "NAME = ". $row['NAME'] ."
  34. ";
  35.       echo "ADDRESS = ". $row['ADDRESS'] ."
  36. ";
  37.       echo "SALARY =  ".$row['SALARY'] ."

  38. ";
  39.    }
  40.    echo "Operation done successfully
  41. ";
  42.    $db->close();
  43. ?>
複製代碼

當上述程序執行時,它會產生以下結果:

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 表:

  1. <?php
  2.    class MyDB extends SQLite3
  3.    {
  4.       function __construct()
  5.       {
  6.          $this->open('test.db');
  7.       }
  8.    }
  9.    $db = new MyDB();
  10.    if(!$db){
  11.       echo $db->lastErrorMsg();
  12.    } else {
  13.       echo "Opened database successfully
  14. ";
  15.    }
  16.    $sql =<<<EOF
  17.       DELETE from COMPANY where ID=2;
  18. EOF;
  19.    $ret = $db->exec($sql);
  20.    if(!$ret){
  21.      echo $db->lastErrorMsg();
  22.    } else {
  23.       echo $db->changes(), " Record deleted successfully
  24. ";
  25.    }

  26.    $sql =<<<EOF
  27.       SELECT * from COMPANY;
  28. EOF;
  29.    $ret = $db->query($sql);
  30.    while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
  31.       echo "ID = ". $row['ID'] . "
  32. ";
  33.       echo "NAME = ". $row['NAME'] ."
  34. ";
  35.       echo "ADDRESS = ". $row['ADDRESS'] ."
  36. ";
  37.       echo "SALARY =  ".$row['SALARY'] ."

  38. ";
  39.    }
  40.    echo "Operation done successfully
  41. ";
  42.    $db->close();
  43. ?>
複製代碼

當上述程序執行時,它會產生以下結果:

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


如要單純查詢表內的紀錄
  1. $db = new SQLite3($db_path);

  2.         $res = $db->query("select * from company;");
  3.         while ($row = $res->fetchArray()) {
  4.                 echo "{$row['ID']} {$row['NAME']} {$row['AGE']} {$row['ADDRESS']} {$row['SALARY']}\n";
  5.         }
複製代碼

文章出處


 

臉書網友討論
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2019-9-20 19:57 , Processed in 0.057583 second(s), 21 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表