SQLite PHP接口详解

小云云
Lepaskan: 2023-03-20 20:48:01
asal
1833 orang telah melayarinya

本文主要和大家分享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";
   }
?>
Salin selepas log masuk

创建表

<?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();
?>
Salin selepas log masuk

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();
?>
Salin selepas log masuk

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();
?>
Salin selepas log masuk

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();
?>
Salin selepas log masuk

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();
?>
Salin selepas log masuk

相关推荐:

php接口如何正确使用

PHP接口的使用技巧

对php接口使用问题的一些总结

Atas ialah kandungan terperinci SQLite PHP接口详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan