PHP
  Home arrow PHP arrow Page 2 - Introduction to Using SQLite with PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Introduction to Using SQLite with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 17
    2006-12-04


    Table of Contents:
  • Introduction to Using SQLite with PHP 5
  • The basics of SQLite
  • Retrieving rows from a database table
  • Examining a few more row-fetching methods

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Introduction to Using SQLite with PHP 5 - The basics of SQLite
    ( Page 2 of 4 )

    As I said right at the beginning, SQLite allows you to work with a full-featured relational database mechanism, with an important difference from MySQL. With MySQL, you would normally appeal to a separate server. With SQLite, the library uses the file system to create the corresponding sets of databases and tables. Memory-based databases are also fully supported by SQLite, but this topic will be covered in upcoming articles of the series.

    To begin with, let me show you a simple example which demonstrates the complete process. This example is aimed at creating a sample database called “db.sqlite,” defining a “USERS” table, and finally fetching some rows from it using an object-oriented approach.

    Here is the example in question, therefore take a look at it: 

    // create new database using the OOP approach

    $db=new SQLiteDatabase("db.sqlite");

    // create table 'USERS' and insert sample data

    $db->query("BEGIN;

            CREATE TABLE users (id INTEGER(4) UNSIGNED PRIMARY KEY,
    name CHAR(255), email CHAR(255));

            INSERT INTO users (id,name,email) VALUES
    (NULL,'User1','user1@domain.com');

            INSERT INTO users (id,name,email) VALUES
    (NULL,'User2','user2@domain.com');

            INSERT INTO users (id,name,email) VALUES
    (NULL,'User3','user3@domain.com');

            COMMIT;");

    // fetch rows from the 'USERS' database table

    $result=$db->query("SELECT * FROM users");

    // loop over rows of database table

    while($result->valid()) {

        // fetch current row

        $row=$result->current();

        print_r($row);

        // move pointer to next row

        $result->next();

    }

    As you’ll surely realize, the above code listing shows some new and interesting things that need to be properly explained with reference to the basic implementation of SQLite. First, notice how the “db.sqlite” database is created via the respective SQLite constructor, and then some trivial rows are inserted into the sample “USERS” table by the intuitive “query()” method.

    You can see that the previous database has been defined on the file system (note the specification of the respective “db.sqlite” file). Additionally, the creation of the “USERS” table, as well as the insertion of sample rows, has been performed in a single step, since SQLite supports the transactional model.

    Once the prior database table has been populated with basic data, rows are returned via the proper execution of a regular SELECT statement. Of course, it’s important to notice here how the “query()” methods return a result set object, which is used inside a “while” loop to display the corresponding database rows. These rows are shown below:

    Array ( [0] => 1 [id] => 1 [1] => User1 [name] => User1 [2] =>
    user1@domain.com [email] => user1@domain.com ) Array ( [0] => 2
    [id] => 2 [1] => User2 [name] => User2 [2] => user2@domain.com
    [email] => user2@domain.com ) Array ( [0] => 3 [id] => 3 [1] =>
    User3 [name] => User3 [2] => user3@domain.com [email] =>
    user3@domain.com )

    Also, for this example, I used some additional methods, like “valid()”, “current()” and “next()” respectively, to traverse the mentioned result set, as I’d proceed with a regular array structure. Therefore, it’s clear to see here how the implementation of a data set iterator is only a matter of using the correct methods.

    Right, at this point you hopefully learned how to create a simple database with SQLite, as well as how to define a database table and run some queries against it. That was really simple, wasn’t it? Now, it’s time to explore a few more handy methods that come embedded with this library.

    To learn how these brand new methods will be defined, keep reading.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek