TinySQLite

A tiny PHP class for use SQLite3 databases

(c) 2019 Alfonso Saavedra “Son Link”

http://son-link.github.io

Under the GNU/GPL 3 or newer license

Install:

Download tinysqlite.php and include on your project:

require_once 'tinysqlite.php'

Initialization

Create a new class instance and pass the file name contains the SQLite3 database:

$db = new TinySQLite('file')

Public variables:

Functions:

query:

Execute any SQL sentence:

$db->query($sql, $values=array()):

Params:

Returns: Boolean indicate if the query is executed correctly

Example:
$db->query('SELECT * FROM table');
$db->query('SELECT * FROM table WHERE id=?', 1);

// Obtein the result for a SELECT
$db->result;
// Or the last insert ID if the table contains a AUTO INCREMENT column:
$db->$lastInsertId

select:

Execute a SELECT sentence:

$db->select($params=array()):

Params:

Returns: associative array with the result or false if the query fails

Example:
// Get all users
$result = $db->select(array('table' => 'users'));

// Get only the user with a specific username
$result = $db->select(
	array('
		'table' => 'users',
		'conditions' => array ('username' => 'son-link')
	')
);

// Get all users order by username
$result = $db->select(
	array('
		'table' => 'users',
		'orderby' => 'username ASC'
	')
);

insert:

Insert new row on the database

$db->insert($table, $values):

Params:

Returns: A int value with the last insert id (if the table contains a PRIMARY KEY with AUTO_INCREMENT) or false if the query fails.

Example:
$insert = $db->insert('users', array(
	'username' => 'son-link',
	'passwd' => 12345,
	'email' => 'son-link@myhost.com'
));

update:

Update row(s)

$db->update($table, $values, $conditions=''):

Params:

Returns: Boolean indicate if the update is executed correctly

Example:
$update = $db->update(
	'users',
	array('email' => 'son-link@example.com'),
	array('id' => 1)
);

delete:

Delete row(s)

$db->delete($table, $conditions, $limit=''):

Params:

Returns: Boolean indicate if the delete is executed correctly

Example:
$delete = $db->delete(
	'users', array('user' => 'son-link'),
);