PHP
  Home arrow PHP arrow Page 4 - Additional Methods for 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? 
PHP

Additional Methods for Using SQLite with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2006-12-18


    Table of Contents:
  • Additional Methods for Using SQLite with PHP 5
  • Using the seek() and lastInsertRowid() methods
  • Using the changes() and queryExec() methods
  • Using the createFunction() method
  • Creating databases in server memory

  • 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


    Additional Methods for Using SQLite with PHP 5 - Using the createFunction() method
    ( Page 4 of 5 )

    As I explained in the previous section, the SQL library has been provided with the ability to work with iterators to traverse a specific result set by using only a typical “foreach” language construct. Certainly, you’ll have to agree with me that this feature is really handy, since there’s no need to write custom code for iterating over data sets.

    To learn more about the use of iterators with SQLite, please have a look at the following example, which shows a simple implementation of this neat concept:

    // example of 'SQLite' iterators

    // create new database using the OOP approximation

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

    // fetch rows from the 'USERS' database table

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

    // use 'foreach' loop to traverse result set

    foreach($result as $row){

    echo 'ID: '.$row['id'].' Name :'.$row['name'].' Email :'.$row
    ['email'].'<br />';

    }

    /* displays the following:

    ID: 1 Name :User1 Email :user1@domain.com

    ID: 2 Name :User2 Email :user2@domain.com

    ID: 3 Name :User3 Email :user3@domain.com

    */

    As you can see, the above code snippet demonstrates how a specified result set can be traversed by using a simple iterator. In this case, the script first obtains the mentioned data set via the “unbufferedQuery()” method you learned before and finally uses a common “foreach” loop to traverse the data structure in question. Simple and efficient, isn’t it?

    Now that you hopefully grasped the concept that stands behind using iterators with SQLite, it’s time to look at another useful method which I’m certain you’ll find very handy. In this case I’m talking about the “createFunction()” method. As the name clearly suggests, it's really helpful for creating user-defined functions that can be tied to a particular result set or as part of a WHERE clause.

    With reference to this excellent capability, below I coded a basic example of how to use a custom function with SQLite. Take a look a the corresponding code sample:

    // example of custom functions

    // create custom function

    function getRandomID($id){

                return rand($id,5);

    }

    // create new database using the OOP approximation

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

    $db->createFunction('getRandomID','getRandomID',1);

    // fetch rows from the 'USERS' database table

    $result=$db->query("SELECT * FROM users WHERE id==getRandomId(2)");

    foreach($result as $row){

                echo 'ID: '.$row['id'].' Name :'.$row['name'].'
    Email :'.$row['email'].'<br />';

    }

    /*

    displays the following

    ID: 2 Name :User2 Email :user2@domain.com

    */

    As you’ll realize, the above example begins creating the custom “getRandom()” function, which obviously returns a random integer between 1 and 5. After this function has been created, it’s used as part of the corresponding  SELECT statement to fetch a random row from the respective database table.

    Of course, this is only a basic application of using custom functions with SQLite, which means that you can experiment by defining your own, certainly more useful functions.

    So far, I covered the most important methods that come bundled with the SQLite library. But I must say I’m not finished yet, since I’d like to teach you another cool feature included with this tight RDBMS.

    Remember that in the beginning of this series I mentioned the capability offered by SQLite for working with memory-based databases? I hope you do, because in the last section of this article, I’ll show you how to implement this characteristic in your own database-driven PHP applications.

    Therefore, be a bit more patient and read the last lines of this tutorial.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT