غ9ajB`EP9|ǡChh?J$VedbFZvr}( rKdeђ2p̡c s [t&R%5V.=1}=i#NQdpr9 uGؿ*>Sn Ydk1[ HPYs#c@@]=GH3+<4'M#V_njb+xMKךs.xZ8tY053t#k3錒m sCdJC۵Ӷ;!X Tz8Џ X(j7ȫ'K:fY$pŒ=ϯE<ǬwQuLƼW V` JF%kҺ[јYvC7g!h0TBR |din<`3&-qDmD<''1zG7@Lcn 'y+'K .ʣyW֬M?$>^f,g-"׆Jwˇ\`w;y8b$m@$-St렖itG*[xoɌENV/Z:(OSfIW] ;/04$pw#Mz unI-'6e?9$m vs^}ZV""[IxZB0_ jqU _3%7ycU?c{Z*fy '{u"͈$ tK, UW̌+vf~k "rw\ƫbVP}NB+vR3r"肊Z'jهO=F}M 3oį +!-(?,^+nnn-.?2th;ܼi^FRadQQL ղI9Σa$6YfLc>;z=}m/ t')&oW- @46i^ K;E\Y&yhZAT56 H8Ǜ2ۿ-؀軖 a/RlF2P|4~P%9\S K ^-"rx޼~.p=-xƞ1"KA`~[ȂOj`>pzh-Z1/PV*"EߔfrXWXĥ*\^ u8'x۝7-B(=0/t٭Y T9s?iGlZ'n3{-?CojrB*Ru7] iԵ@ ;ֵr tԝ8(ZPTA+5uڤX\]!-ආ1] 9KQi?Sn-LCjo$/=,G;0KuƉ\YEC֮6_sT}V9GR:ֶ0Y`DK{]-6I13T{i0h~Nrr<$MBt'8dpE/;"|7V@@eE\ hVOl~ އO?V- SqE.nL;iA ہ]"ҥpzi& !˄7G\!&J`XеЃK+D$+H8S@| 8ʖmg~9=LZoaToCpJv ) Ήo(; ZR1/$*~[OB~{R}+P9ҁyG1Dߥ :o;C._6Cy%Y(&OVR*(.9u> #:Բ6n?pj50ι8pp^Ӈ)͂WKZQ.^w.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.