PHP連線SQLite資料庫


SQLite3擴充套件名在PHP 5.3.0+以上都會預設啟用。可以在編譯時使用--without-sqlite3來禁用它。

Windows使用者可通過啟用php_sqlite3.dll才能使用此擴充套件。 php_sqlite3.dll預設包含在PHP 5.3.0之後的PHP發行版中。

有關詳細的安裝說明,請檢視PHP教學及其官方網站。

連線到SQLite資料庫

以下PHP程式碼顯示如何連線到SQLite資料庫。 如果資料庫不存在,那麼它將建立一個新的資料庫,最後將返回一個資料庫物件。

<?php
   class SQLiteDB extends SQLite3
   {
      function __construct()
      {
         $this->open('phpdb.db');
      }
   }
   $db = new SQLiteDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Yes, Opened database successfully\n";
   }
?>

現在,執行上面的程式在當前目錄中建立指定資料庫:phpdb.db。可以根據需要更改路徑。 如果資料庫成功建立,則會提供以下訊息:

在執行上面語句後,應該也會在相同的目錄下自動建立一個名稱為:phpdb.db的資料庫檔案。如下所示 -

建立表

以下PHP程式將用於在上面建立的資料庫(phpdb.db)中建立一個表:

<?php
class SQLiteDB extends SQLite3
{
  function __construct()
  {
     $this->open('phpdb.db');
  }
}
$db = new SQLiteDB();
if(!$db){
  echo $db->lastErrorMsg();
} else {
  echo "Yes, Opened database successfully<br/>\n";
}

$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 "Yes, Table created successfully<br/>\n";
}
$db->close();

當執行上述程式時,它將在資料庫(phpdb.db)中建立一個名稱為:company的表,並顯示以下訊息:

插入資料操作

以下PHP程式顯示了如何在上述範例中建立的company表中插入資料記錄:

<?php
class SQLiteDB extends SQLite3
{
  function __construct()
  {
     $this->open('phpdb.db');
  }
}
$db = new SQLiteDB();
if(!$db){
  echo $db->lastErrorMsg();
} else {
  echo "Yes, Opened database successfully<br/>\n";
}

// 先刪除後建立表
$sql = "DROP table company";
$ret = $db->exec($sql);

// 建立表語句

$sql =<<<EOF
      CREATE TABLE if not exists 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 "Yes, Table created successfully<br/>\n";
}

// $db->close();

$sql =<<<EOF
      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (1, 'Maxsu', 26, 'Haikou', 20000.00 );

      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (2, 'Allen', 25, 'Guangzhou', 15000.00 );

      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (3, 'Tenny', 23, 'Shanghai', 20000.00 );

      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (4, 'Weiwang', 25, 'Beijing ', 65000.00 );
EOF;

   $ret = $db->exec($sql);
   if(!$ret){
      echo $db->lastErrorMsg();
   } else {
      echo "Yes, Some Records has Inserted successfully<br/>\n";
   }
   $db->close();

當執行上述程式時,將向company表中插入給定的記錄,並顯示如下:

SELECT操作

以下PHP程式顯示了如何從上述範例中建立的company表中獲取並顯示資料記錄:

<?php
class SQLiteDB extends SQLite3
{
  function __construct()
  {
     $this->open('phpdb.db');
  }
}
$db = new SQLiteDB();
if(!$db){
  echo $db->lastErrorMsg();
} else {
  echo "Yes, Opened database successfully<br/>\n";
}

// 先刪除後建立表
$sql = "DROP table company";
$ret = $db->exec($sql);

// 建立表

$sql =<<<EOF
      CREATE TABLE if not exists 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 "Yes, Table created successfully<br/>\n";
}

// $db->close();

$sql =<<<EOF
      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (1, 'Maxsu', 26, 'Haikou', 20000.00 );

      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (2, 'Allen', 25, 'Guangzhou', 15000.00 );

      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (3, 'Tenny', 23, 'Shanghai', 20000.00 );

      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (4, 'Weiwang', 25, 'Beijing ', 65000.00 );
EOF;

$ret = $db->exec($sql);
if(!$ret){
  echo $db->lastErrorMsg();
} else {
  echo "Yes, Some Records has Inserted successfully<br/>\n";
}

// 查詢表中的資料

echo "<b> Select Data from company table :</b><hr/>";

$sql =<<<EOF
  SELECT * from COMPANY;
EOF;

$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
  echo "ID = ". $row['ID'] . "<br/>\n";
  echo "NAME = ". $row['NAME'] ."<br/>\n";
  echo "ADDRESS = ". $row['ADDRESS'] ."<br/>\n";
  echo "SALARY =  ".$row['SALARY'] ."<br/>\n\n";
  echo '----------------------------------<br/>';
}

echo "Operation done successfully\n";

$db->close();

執行上述程式時,會產生以下結果:

更新操作

以下PHP程式碼顯示了如何使用UPDATE語句來更新記錄,然後從company表中獲取並顯示更新的記錄:

<?php
class SQLiteDB extends SQLite3
{
  function __construct()
  {
     $this->open('phpdb.db');
  }
}
$db = new SQLiteDB();
if(!$db){
  echo $db->lastErrorMsg();
} else {
  echo "Yes, Opened database successfully<br/>\n";
}

// 先刪除後建立表
$sql = "DROP table company";
$ret = $db->exec($sql);

// 建立表

$sql =<<<EOF
      CREATE TABLE if not exists 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 "Yes, Table created successfully<br/>\n";
}

// $db->close();

$sql =<<<EOF
      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (1, 'Maxsu', 26, 'Haikou', 20000.00 );

      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (2, 'Allen', 25, 'Guangzhou', 15000.00 );

      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (3, 'Tenny', 23, 'Shanghai', 20000.00 );

      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (4, 'Weiwang', 25, 'Beijing ', 65000.00 );
EOF;

$ret = $db->exec($sql);
if(!$ret){
  echo $db->lastErrorMsg();
} else {
  echo "Yes, Some Records has Inserted successfully<br/>\n";
}

// 更新ID=1的薪水為:29999

$sql = 'UPDATE COMPANY set SALARY = 29999.00 where ID=1';
$ret = $db->exec($sql);
if(!$ret){
  echo $db->lastErrorMsg();
} else {
  echo $db->changes(), " Record(ID=1) updated successfully<br/>\n";
}


// 查詢表中的資料

echo "<b> Select Data from company table :</b><hr/>";

$sql =<<<EOF
  SELECT * from COMPANY;
EOF;

$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
  echo "ID = ". $row['ID'] . "<br/>\n";
  echo "NAME = ". $row['NAME'] ."<br/>\n";
  echo "ADDRESS = ". $row['ADDRESS'] ."<br/>\n";
  echo "SALARY =  ".$row['SALARY'] ."<br/>\n\n";
  echo '----------------------------------<br/>';
}

echo "Operation done successfully\n";

$db->close();

執行上述程式時,會產生以下結果:

刪除操作

以下PHP程式碼顯示了如何使用DELETE語句刪除任何記錄,然後從company表中獲取並顯示剩餘的記錄:

<?php
class SQLiteDB extends SQLite3
{
  function __construct()
  {
     $this->open('phpdb.db');
  }
}
$db = new SQLiteDB();
if(!$db){
  echo $db->lastErrorMsg();
} else {
  echo "Yes, Opened database successfully<br/>\n";
}

// 先刪除後建立表
$sql = "DROP table company";
$ret = $db->exec($sql);

// 建立表

$sql =<<<EOF
      CREATE TABLE if not exists 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 "Yes, Table created successfully<br/>\n";
}

// $db->close();

$sql =<<<EOF
      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (1, 'Maxsu', 26, 'Haikou', 20000.00 );

      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (2, 'Allen', 25, 'Guangzhou', 15000.00 );

      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (3, 'Tenny', 23, 'Shanghai', 20000.00 );

      INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (4, 'Weiwang', 25, 'Beijing ', 65000.00 );
EOF;

$ret = $db->exec($sql);
if(!$ret){
  echo $db->lastErrorMsg();
} else {
  echo "Yes, Some Records has Inserted successfully<br/>\n";
}

// 更新ID小於等於2的資料記錄

$sql =<<<EOF
  DELETE from COMPANY where ID<=2;
EOF;
$ret = $db->exec($sql);
if(!$ret){
 echo $db->lastErrorMsg();
} else {
  echo $db->changes(), " Record(ID<=2) deleted successfully<br/>\n";
}

// 查詢表中的資料

echo "<b> Select Data from company table :</b><hr/>";

$sql =<<<EOF
  SELECT * from COMPANY;
EOF;

$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
  echo "ID = ". $row['ID'] . "<br/>\n";
  echo "NAME = ". $row['NAME'] ."<br/>\n";
  echo "ADDRESS = ". $row['ADDRESS'] ."<br/>\n";
  echo "SALARY =  ".$row['SALARY'] ."<br/>\n\n";
  echo '----------------------------------<br/>';
}

echo "Operation done successfully\n";

$db->close();

執行上述程式時,會產生以下結果:

由於其它操作都類似,只需寫上對應SQL語句,然後使用PHP執行即可,所以其它的範例在這裡就不再一一演示了。如有疑問,可以留言聯絡。