SQLite PHP介面詳解

小云云
發布: 2023-03-20 20:48:01
原創
1819 人瀏覽過

本文主要跟大家分享SQLite PHP介面知識,希望能幫助大家,首先我們來看看PHP 介面 API的相關知識。

PHP 介面API


#連接資料庫

<?php
   class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open(&#39;test.db&#39;);
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }
?>
登入後複製

建立表格

<?php
   class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open(&#39;test.db&#39;);
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\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 "Table created successfully\n";
   }
   $db->close();
?>
登入後複製

INSERT 操作

<?php
   class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open(&#39;test.db&#39;);
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }

   $sql =<<<EOF
      INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (1, &#39;Paul&#39;, 32, &#39;California&#39;, 20000.00 );

      INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (2, &#39;Allen&#39;, 25, &#39;Texas&#39;, 15000.00 );

      INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (3, &#39;Teddy&#39;, 23, &#39;Norway&#39;, 20000.00 );

      INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (4, &#39;Mark&#39;, 25, &#39;Rich-Mond &#39;, 65000.00 );
EOF;

   $ret = $db->exec($sql);
   if(!$ret){
      echo $db->lastErrorMsg();
   } else {
      echo "Records created successfully\n";
   }
   $db->close();
?>
登入後複製

SELECT 操作

<?php
   class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open(&#39;test.db&#39;);
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }

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

   $ret = $db->query($sql);
   while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
      echo "ID = ". $row[&#39;ID&#39;] . "\n";
      echo "NAME = ". $row[&#39;NAME&#39;] ."\n";
      echo "ADDRESS = ". $row[&#39;ADDRESS&#39;] ."\n";
      echo "SALARY =  ".$row[&#39;SALARY&#39;] ."\n\n";
   }
   echo "Operation done successfully\n";
   $db->close();
?>
登入後複製

UPDATE 操作

<?php
   class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open(&#39;test.db&#39;);
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }
   $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\n";
   }

   $sql =<<<EOF
      SELECT * from COMPANY;
EOF;
   $ret = $db->query($sql);
   while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
      echo "ID = ". $row[&#39;ID&#39;] . "\n";
      echo "NAME = ". $row[&#39;NAME&#39;] ."\n";
      echo "ADDRESS = ". $row[&#39;ADDRESS&#39;] ."\n";
      echo "SALARY =  ".$row[&#39;SALARY&#39;] ."\n\n";
   }
   echo "Operation done successfully\n";
   $db->close();
?>
登入後複製

DELETE 操作

<?php
   class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open(&#39;test.db&#39;);
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }
   $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\n";
   }

   $sql =<<<EOF
      SELECT * from COMPANY;
EOF;
   $ret = $db->query($sql);
   while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
      echo "ID = ". $row[&#39;ID&#39;] . "\n";
      echo "NAME = ". $row[&#39;NAME&#39;] ."\n";
      echo "ADDRESS = ". $row[&#39;ADDRESS&#39;] ."\n";
      echo "SALARY =  ".$row[&#39;SALARY&#39;] ."\n\n";
   }
   echo "Operation done successfully\n";
   $db->close();
?>
登入後複製

相關建議:

php介面如何正確使用

PHP介面的使用技巧

對php介面使用問題的一些總結

以上是SQLite PHP介面詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!