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O$K^9@hƝͫm r8Ԛ-rn3Ұ\vNrw.sqlite.org/datatype3.html">learn more about SQLite’s built-in datatypes here.

CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT, price REAL);
  • Add some data to the new inventory table using INSERT commands, as shown below:

    INSERT INTO items VALUES ('1001', 'Salt', 3.15);
    INSERT INTO items VALUES ('1002', 'Pepper', 2.75);
    INSERT INTO items VALUES ('1003', 'Eggs', 2.00);
    INSERT INTO items VALUES ('1004', 'Bacon', 7.25);
    INSERT INTO items VALUES ('1005', 'Milk', 1.15);
    INSERT INTO items VALUES ('1006', 'Strawberries', 8.73);
    INSERT INTO items VALUES ('1007', 'Cereal', 2.65);
  • You can now also run a SELECT query on the data. For example, the query below returns all items that cost less than $3:

    SELECT * FROM items WHERE price < 3.00;
    image2
  • Once you’re done using the database, exit it by typing .quit at the sqlite> prompt.

  • The database file (in this example, mydb.sq3) contains all your tables and data, so you should remember to back it up regularly.

    The previous steps discussed how to create and use an SQLite database using the command-line client. However, more often than not, you’ll be using an SQLite database in combination with a PHP-powered Web application. XAMPP includes the PHP SQLite extension, so doing this is not very difficult at all.

    To connect to your SQLite database and execute queries on it with PHP, use your text editor to create an example script named sqlite.php in the htdocs subdirectory of your XAMPP installation directory and fill it with the following code:

    <?php
    $db = new SQLite3('mydb.sq3');
    $sql = "SELECT * FROM items WHERE price < 3.00";
    $result = $db->query($sql);
    while ($row = $result->fetchArray(SQLITE3_ASSOC)){
      echo $row['name'] . ': $' . $row['price'] . '<br/>';
    }
    unset($db);

    The first line of code creates a new SQLite3 object, using the mydb.sq3 database file you created earlier. Then, the object’s query() method is used to execute a SELECT query on the database, and the result object’s fetchArray() method is used to iterate over the result set. Adding the SQLITE3_ASSOC parameter to the fetchArray() method tells PHP to return the results as an associative array, making it easy to access individual fields of the result set and display them on a Web page.

    Once done, save your changes and ensure that your Apache server is running. Then, browse to http://localhost/sqlite.php to execute the script. You should see something like this:

    image3
    To find out more about SQLite’s powerful features, read the SQLite documentation.