SQLite PHP Interface Detailed

Source: Internet
Author: User
This article is mainly to share with you the SQLite PHP interface knowledge, hoping to help everyone, first of all, we look at the PHP interface API related knowledge.

PHP Interface API


Connecting to a database

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

Create a table

<?php   class MyDB extends SQLite3   {      function __construct ()      {         $this->open (' test.db ');      }   }   $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,      SALARY         REAL); EOF;   $ret = $db->exec ($sql);   if (! $ret) {      echo $db->lasterrormsg ();   } else {      echo "Table created successfully\n";   }   $db->close ();? >

INSERT operation

<?php   class MyDB extends SQLite3   {      function __construct ()      {         $this->open (' test.db ');      }   }   $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, ' Paul ', +, ' California ', 20000.00);      INSERT into Company (id,name,age,address,salary)      VALUES (2, ' Allen ', +, ' Texas ', 15000.00);      INSERT into company      VALUES (3, ' Teddy ', id,name,age,address,salary, ' Norway ', 20000.00);      INSERT into Company (id,name,age,address,salary)      VALUES (4, ' Mark ', +, ' Rich-mond ', 65000.00); EOF;   $ret = $db->exec ($sql);   if (! $ret) {      echo $db->lasterrormsg ();   } else {      echo "Records created successfully\n";   }   $db->close ();? >

SELECT operation

<?php   class MyDB extends SQLite3   {      function __construct ()      {         $this->open (' test.db ');      }   }   $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 [' ID ']. "\ n";      echo "NAME =". $row [' NAME ']. \ n ";      echo "ADDRESS =". $row [' ADDRESS ']. \ n ";      echo "SALARY =  ". $row [' SALARY ']. " \ n ";   }   echo "Operation done successfully\n";   $db->close ();? >

UPDATE operation

<?php   class MyDB extends SQLite3   {      function __construct ()      {         $this->open (' test.db ');      }   }   $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 [' ID ']. "\ n";      echo "NAME =". $row [' NAME ']. \ n ";      echo "ADDRESS =". $row [' ADDRESS ']. \ n ";      echo "SALARY =  ". $row [' SALARY ']. " \ n ";   }   echo "Operation done successfully\n";   $db->close ();? >

DELETE operation

<?php class MyDB extends SQLite3 {function __construct () {      $this->open (' test.db ');   }} $db = new MyDB ();   if (! $db) {echo $db->lasterrormsg ();   } else {echo "opened database successfully\n"; } $sql =<<<eof DELETE from the 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 [' ID '].      "\ n"; echo "NAME =". $row [' NAME '].      \ n "; echo "ADDRESS =". $row [' ADDRESS '].      \ n "; echo "SALARY =". $row [' SALARY ']. "   \ n ";   } echo "Operation done successfully\n"; $db->close ();? 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.