You are on page 1of 2

XML to SQLite Part-2

Secondt part: PHP to SQLite


----- -----
The unique advantage of SQLITE is that it is built-in in PHP 5.0, and offers users a lightweight
database system that is fast, efficient and gets the job done in no time. Since it's enabled by default in
PHP 5.0, it provides a viable alternative to MySQL; you can use it out of the box, without spending
time on version checks and library downloads; just install PHP 5 and start going.

Before getting into the code, let's make sure that you have a clear idea of what SQLite is. Unlike
MySQL, SQLite is a file-based database engine and uses file I/O (input/output) functions to store and
read databases from files on disk. It's also much, much smaller than MySQL - the command-line
version of SQLite weighs in at under 200 KB - and supports most of the SQL commands we're used to.
This small size shouldn't deceive you, however - according to the official SQLite Web site, SQLite
supports databases up to 2 terabytes in size and is actually lot faster than MySQL.
SQLite database files are easily portable, and SQLite databases created on Windows work
fine on *NIX / Linux platforms and vice-versa.
One of SQLite's more interesting aspects is that it is completely typeless. Fields in an SQLite database
need not be associated with a specific type, and even if they are, you can still insert values of different
types into them.
In order to use SQLite and PHP together, your PHP build must include SQLite. This is enabled by
default in both the UNIX and Windows versions of PHP 5. If you're a PHP 4.x user, though, don't lose
heart - you can still use SQLite, by manually downloading and installing php_sqlite.dll from
http://snaps.php.net (Windows) or the latest tarball from http://pecl.php.net/package/SQLite (UNIX /
Linux). You don't need to download anything else; the SQLite 'client' is its own engine.

Open a terminal window and then


cd /your-document-root-directory/uploads/
sqlite test
SQLite version 2.8.17
Enter ".help" for instructions
sqlite> create table test1 (var1 varchar(8),var2 varchar(8),var3 varchar(8),var4 varchar(8),var5
varchar(6) );
sqlite>.tables
test1
sqlite>.q
[bera@bera ~]$
This will create an sqlite database 'test' by name. Prior to that you must have write permission for this
directory. The 'create table test1 …...' command will create a table test1 with five variables. The '.table'
command will show the table created by the above command.
From the previous program we have a handful of variables from the xml file.
$first = $cell->nodeValue; // Points to the first column
$second = $cell->nodeValue; // Points to the second column
$third = $cell->nodeValue; // Points to the third column
$fourth = $cell->nodeValue; // Points to the forth column
$fifth = $cell->nodeValue; // Points to the fifth column

Our import.php file will now read these data and then insert into the test.test1 table now.
The file is located here

http://berasomnath.googlepages.com/import.php

For smaller excel file it is easier to read it by PHP and then transfer into database but for very big excel
file it is better to transfer them into xml file and then read it by PHP and then put it into database. The
PHP-5 and above is by default DOM (XML API) enabled.

There are many small free utilities available on internet by which files can be easily transformed into
xml. Else can be done on OpenOffice or Msoffice.

S Bera
Mumbi

You might also like