SQLite

From Phpmaniac

Jump to: navigation, search

Return to Contents

Contents

What is SQLite

SQLite is a lightweight datbase that requires no server. This is because the database engine is written into the driver. This makes it a perfect replacement for storing data in files.

Why use SQLite

Storing data in files is prone to corruption if a single page is accessed more than once simultaneously. SQLite handles all the file locking for you which means that you won't have corruption issues and has the added advantage of being a full database which means that skills gained by using SQLite will be transferable to other database systems.

There is a small overhead in using SQLite, but it is insignificant.

How do we use SQLite

In this example we will access SQLite using PHP PDO. This has the added advantage of being portable so that if you choose to move your database to another back end such as PostgreSQL, the move will only require changing the PDO DSN (Described in PHP PDO).

Creating a database table

Creating a database in SQL is the same as creating a table in other Database management systems.

  1. <?php
  2.         $create_sql = "CREATE TABLE `people` (
  3.                `first_name` text NOT NULL,
  4.                `last_name` text NOT NULL,
  5.                `age` int NOT NULL
  6.                );";                                // Create SQL
  7.  
  8.         $db_conn = new PDO("sqlite:test_database"); // Connect to the database
  9.         $query = $db_conn->prepare($create_sql);    // Create a prepared statement
  10.         $query->execute();                          // Execute the query
  11. ?>

Inserting data

Inserting data into SQLite is the same as inserting data into any other database

  1. <?php
  2.         $insert_sql = "insert into people (first_name, last_name, age) values (:first_name, :last_name, :age)";
  3.  
  4.         $db_conn = new PDO("sqlite:test_database");    // Connect to the database
  5.         $query = $db_conn->prepare($insert_sql);       // Create a prepared statement
  6.  
  7.         $first_name = "Leigh";                      
  8.         $last_name = "Finch";
  9.         $age = 25;
  10.  
  11.         $query->bindParam(":first_name", $first_name); // bind the variable $first_name to :first_name
  12.         $query->bindParam(":last_name", $last_name);   // bind the variable $last_name to :last_name
  13.         $query->bindParam(":age", $age);               // bind the variable $age to :age
  14.  
  15.         $query->execute();                             // Execute the query
  16. ?>

Select data

Select statements are exactly the same as other database systems.

  1. <?php
  2.         $select_sql = "select * from people";          // Simple select squery
  3.  
  4.         $db_conn = new PDO("sqlite:test_database");    // Connect to the database
  5.         $query = $db_conn->prepare($select_sql);       // Create a prepared statement
  6.  
  7.         $query->execute();                             // Execute the query
  8.         while($row = $query->fetch())                  // Fetch the rows one by one
  9.         {
  10.                 var_dump($row);                        // Dump the results to output
  11.         }
  12.  
  13. ?>
array(6) {
  ["first_name"]=>
  string(5) "Leigh"
  [0]=>
  string(5) "Leigh"
  ["last_name"]=>
  string(5) "Finch"
  [1]=>
  string(5) "Finch"
  ["age"]=>
  string(2) "25"
  [2]=>
  string(2) "25"
}

Return to Contents

Personal tools