首页  >  文章  >  后端开发  >  SQLite PHP接口详解

SQLite PHP接口详解

小云云
小云云原创
2018-03-07 09:54:551812浏览

本文主要和大家分享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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn