Home arrow PHP arrow Page 5 - Additional Methods for Using SQLite with PHP 5

Creating databases in server memory - PHP

Welcome to the concluding part of the series “Using SQLite with PHP 5.” As you’ll possibly know, PHP 5 comes equipped with a fully-featured RDBMS called SQLite that definitely can make your life as a PHP developer much easier. It's particularly helpful if you want to get rid of MySQL at least for a time while maintaining the data layer of your application completely isolated from the business logic.

TABLE OF CONTENTS:
  1. Additional Methods for Using SQLite with PHP 5
  2. Using the seek() and lastInsertRowid() methods
  3. Using the changes() and queryExec() methods
  4. Using the createFunction() method
  5. Creating databases in server memory
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
December 18, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The last SQLite feature that I plan to cover here concerns specifically the creation of databases in server memory, instead of using the conventional file system. As you can imagine, this type of database can be used (among other situations) in those cases where you need to have at your disposal a fully-structured database relational system, but your data will be rather temporary, at least during the execution of your application.

That being said, defining a memory-based database with SQLite is reduced to code something as simple as this:

// example using memory-based database

// create a new memory-based database

$db = new SQLiteDatabase(":memory:");

// create table 'USERS' and insert some data

$db->query("BEGIN;

        CREATE TABLE users (id INTEGER PRIMARY KEY, name VARCHAR
(255),email VARCHAR(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');

        COMMIT;");

// display number of affected rows after the insertion

echo $db->changes().' rows affected by the insertion<br />';

// display ID of last inserted row

echo "ID of last inserted row is: ".$db->lastInsertRowid();

/*

displays the following

2 rows affected by the insertion

ID of last inserted row is: 2

*/

As shown above, a new database has been created in memory by simply specifying the “:memory” argument for the corresponding SQLite constructor. After this process has been performed, I defined a “USERS” table, in addition to inserting some trivial data, and finally displayed the ID that corresponds to the last inserted row.

As I always suggest, try creating different memory-based databases and watch what happens in each case. The process is truly educational.

Final thoughts

We’ve come to the end of this series. In these three consecutive tutorials, I took an in-depth look at the most relevant methods that come with the SQLite RDBMS, which has been included with PHP 5.

As you learned here, if your database-driven application doesn’t require all the features offered by MySQL, or another RDBMS, then this tight yet powerful library is worth considering.

See you in the next PHP article!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: