php SQLite

New in PHP version 5 is the compact and small database connection called SQLite. As its name suggests, it is a small and lightweight database tool. This database product comes with PHP 5 and is now available in PHP by default. SQLite is ready to go right out of the box when you install PHP, so if you are looking for a lightweight and compact database tool, be sure to read up on SQLite

There is an OOP interface to SQLite, so you can instantiate an object with the following statement:

$db = new SQLiteDatabase(“c:/copy/library.sqlite”);

Example of SQLite

$sql = "CREATE TABLE 'authors' ('authorid' INTEGER PRIMARY KEY, 'name' TEXT)";
if (!$database->queryExec($sql, $error)) {
 echo "Create Failure - {$error}<br />";
}
else {
 echo "Table Authors was created <br />";
}
$sql = <<<SQL
INSERT INTO 'authors' ('name') VALUES ('J.R.R. Tolkien');
INSERT INTO 'authors' ('name') VALUES ('Alex Haley');
INSERT INTO 'authors' ('name') VALUES ('Tom Clancy');
INSERT INTO 'authors' ('name') VALUES ('Isaac Asimov');
SQL;
if (!$database->queryExec($sql, $error)) {
 echo "Insert Failure - {$error}<br />";
}
else {
 echo "INSERT to Authors - OK<br />";
}

Table Authors was created
INSERT to Authors – OK

Data types available in SQLite

Data typeExplanation
TextStores data as NULL, TEXT, or BLOB content. If a number is supplied to a text field, it is converted to text before
it is stored.
NumericCan store either integer or real data. If text data is supplied, an attempt is made to convert the information to
numerical format.
IntegerBehaves the same as the numeric data type. However, if data of real format is supplied, it is stored as an integer.
This may affect data storage accuracy
RealBehaves the same as the numeric data type, except that it forces integer values into floating-point representation.
NoneThis is a catchall data type. This type does not prefer one base type to another. Data is stored exactly as supplied
Data types available in SQLite

Leave a Comment