PHP
  Home arrow PHP arrow Page 2 - Introduction to Using SQLite with PHP ...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Introduction to Using SQLite with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 12
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · If you're using PHP 5 to develop your database-driven web applications, then you'll...
       · need a comma here: CREATE TABLE users (id INTEGER(4), UNSIGNED PRIMARY...
       · Thanks for the comments on my PHP article, but the SQL syntax is...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT