class SQLite3 (View source)

A class that interfaces SQLite 3 databases.

Constants

OK

DENY

IGNORE

CREATE_INDEX

CREATE_TABLE

CREATE_TEMP_INDEX

CREATE_TEMP_TABLE

CREATE_TEMP_TRIGGER

CREATE_TEMP_VIEW

CREATE_TRIGGER

CREATE_VIEW

DELETE

DROP_INDEX

DROP_TABLE

DROP_TEMP_INDEX

DROP_TEMP_TABLE

DROP_TEMP_TRIGGER

DROP_TEMP_VIEW

DROP_TRIGGER

DROP_VIEW

INSERT

PRAGMA

READ

SELECT

TRANSACTION

UPDATE

ATTACH

DETACH

ALTER_TABLE

REINDEX

ANALYZE

CREATE_VTABLE

DROP_VTABLE

FUNCTION

SAVEPOINT

COPY

RECURSIVE

Methods

void
open(string $filename, int $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, string $encryptionKey = '')

Opens an SQLite database

bool
close()

Closes the database connection

bool
exec(string $query)

Executes a result-less query against a given database

static array
version()

Returns the SQLite3 library version as a string constant and as a number

int
lastInsertRowID()

Returns the row ID of the most recent INSERT into the database

int
lastErrorCode()

Returns the numeric result code of the most recent failed SQLite request

string
lastErrorMsg()

Returns English text describing the most recent failed SQLite request

bool
busyTimeout(int $milliseconds)

Sets the busy connection handler

bool
loadExtension(string $name)

Attempts to load an SQLite extension library

int
changes()

Returns the number of database rows that were changed (or inserted or deleted) by the most recent SQL statement

static string
escapeString(string $string)

Returns a string that has been properly escaped

SQLite3Stmt|false
prepare(string $query)

Prepares an SQL statement for execution

SQLite3Result|false
query(string $query)

Executes an SQL query

mixed
querySingle(string $query, bool $entireRow = false)

Executes a query and returns a single result

bool
createFunction(string $name, mixed $callback, int $argCount = -1, int $flags = 0)

Registers a PHP function for use as an SQL scalar function

bool
createAggregate(string $name, mixed $stepCallback, mixed $finalCallback, int $argCount = -1)

Registers a PHP function for use as an SQL aggregate function

bool
createCollation(string $name, callable $callback)

Registers a PHP function for use as an SQL collating function

resource|false
openBlob(string $table, string $column, int $rowid, string $database = 'main', int $flags = SQLITE3_OPEN_READONLY)

Opens a stream resource to read a BLOB

bool
enableExceptions(bool $enable = false)

Enable throwing exceptions

__construct(string $filename, int $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, string $encryptionKey = '')

Instantiates an SQLite3 object and opens an SQLite 3 database

int
lastExtendedErrorCode()

No description

bool
enableExtendedResultCodes(bool $enable = true)

No description

bool
backup(SQLite3 $destination, string $sourceDatabase = 'main', string $destinationDatabase = 'main')

No description

bool
setAuthorizer(callable|null $callback)

No description

Details

void open(string $filename, int $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, string $encryptionKey = '')

Opens an SQLite database

Parameters

string $filename

Path to the SQLite database, or :memory: to use in-memory database.

int $flags

Optional flags used to determine how to open the SQLite database. By default, open uses SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE.

SQLITE3_OPEN_READONLY: Open the database for reading only.

string $encryptionKey

An optional encryption key used when encrypting and decrypting an SQLite database.

Return Value

void

No value is returned.

bool close()

Closes the database connection

Return Value

bool

TRUE on success, FALSE on failure.

bool exec(string $query)

Executes a result-less query against a given database

Parameters

string $query

The SQL query to execute (typically an INSERT, UPDATE, or DELETE query).

Return Value

bool

TRUE if the query succeeded, FALSE on failure.

static array version()

Returns the SQLite3 library version as a string constant and as a number

Return Value

array

an associative array with the keys "versionString" and "versionNumber".

int lastInsertRowID()

Returns the row ID of the most recent INSERT into the database

Return Value

int

the row ID of the most recent INSERT into the database

int lastErrorCode()

Returns the numeric result code of the most recent failed SQLite request

Return Value

int

an integer value representing the numeric result code of the most recent failed SQLite request.

string lastErrorMsg()

Returns English text describing the most recent failed SQLite request

Return Value

string

an English string describing the most recent failed SQLite request.

bool busyTimeout(int $milliseconds)

Since: 5.3.3

Sets the busy connection handler

Parameters

int $milliseconds

The milliseconds to sleep. Setting this value to a value less than or equal to zero, will turn off an already set timeout handler.

Return Value

bool

TRUE on success, FALSE on failure.

bool loadExtension(string $name)

Attempts to load an SQLite extension library

Parameters

string $name

The name of the library to load. The library must be located in the directory specified in the configure option sqlite3.extension_dir.

Return Value

bool

TRUE if the extension is successfully loaded, FALSE on failure.

int changes()

Returns the number of database rows that were changed (or inserted or deleted) by the most recent SQL statement

Return Value

int

an integer value corresponding to the number of database rows changed (or inserted or deleted) by the most recent SQL statement.

static string escapeString(string $string)

Returns a string that has been properly escaped

Parameters

string $string

The string to be escaped.

Return Value

string

a properly escaped string that may be used safely in an SQL statement.

SQLite3Stmt|false prepare(string $query)

Prepares an SQL statement for execution

Parameters

string $query

The SQL query to prepare.

Return Value

SQLite3Stmt|false

an SQLite3Stmt object on success or FALSE on failure.

SQLite3Result|false query(string $query)

Executes an SQL query

Parameters

string $query

The SQL query to execute.

Return Value

SQLite3Result|false

an SQLite3Result object, or FALSE on failure.

mixed querySingle(string $query, bool $entireRow = false)

Executes a query and returns a single result

Parameters

string $query

The SQL query to execute.

bool $entireRow

[optional]

By default, querySingle returns the value of the first column returned by the query. If entire_row is TRUE, then it returns an array of the entire first row.

Return Value

mixed

the value of the first column of results or an array of the entire first row (if entire_row is TRUE).

If the query is valid but no results are returned, then NULL will be returned if entire_row is FALSE, otherwise an empty array is returned.

Invalid or failing queries will return FALSE.

bool createFunction(string $name, mixed $callback, int $argCount = -1, int $flags = 0)

Registers a PHP function for use as an SQL scalar function

Parameters

string $name

Name of the SQL function to be created or redefined.

mixed $callback

The name of a PHP function or user-defined function to apply as a callback, defining the behavior of the SQL function.

int $argCount

The number of arguments that the SQL function takes. If this parameter is negative, then the SQL function may take any number of arguments.

int $flags

A bitwise conjunction of flags. Currently, only SQLITE3_DETERMINISTIC is supported, which specifies that the function always returns the same result given the same inputs within a single SQL statement.

Return Value

bool

TRUE upon successful creation of the function, FALSE on failure.

bool createAggregate(string $name, mixed $stepCallback, mixed $finalCallback, int $argCount = -1)

Registers a PHP function for use as an SQL aggregate function

Parameters

string $name

Name of the SQL aggregate to be created or redefined.

mixed $stepCallback

The name of a PHP function or user-defined function to apply as a callback for every item in the aggregate.

mixed $finalCallback

The name of a PHP function or user-defined function to apply as a callback at the end of the aggregate data.

int $argCount

[optional]

The number of arguments that the SQL aggregate takes. If this parameter is negative, then the SQL aggregate may take any number of arguments.

Return Value

bool

TRUE upon successful creation of the aggregate, FALSE on failure.

bool createCollation(string $name, callable $callback)

Since: 5.3.11

Registers a PHP function for use as an SQL collating function

Parameters

string $name

Name of the SQL collating function to be created or redefined

callable $callback

The name of a PHP function or user-defined function to apply as a callback, defining the behavior of the collation. It should accept two strings and return as strcmp does, i.e. it should return -1, 1, or 0 if the first string sorts before, sorts after, or is equal to the second.

Return Value

bool

TRUE on success or FALSE on failure.

resource|false openBlob(string $table, string $column, int $rowid, string $database = 'main', int $flags = SQLITE3_OPEN_READONLY)

Opens a stream resource to read a BLOB

Parameters

string $table

The table name.

string $column

The column name.

int $rowid

The row ID.

string $database

[optional]

The symbolic name of the DB

int $flags

[optional]

Either SQLITE3_OPEN_READONLY or SQLITE3_OPEN_READWRITE to open the stream for reading only, or for reading and writing, respectively.

Return Value

resource|false

Returns a stream resource, or FALSE on failure.

bool enableExceptions(bool $enable = false)

Enable throwing exceptions

Parameters

bool $enable

Return Value

bool

Returns the old value; true if exceptions were enabled, false otherwise.

__construct(string $filename, int $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, string $encryptionKey = '')

Instantiates an SQLite3 object and opens an SQLite 3 database

Parameters

string $filename

Path to the SQLite database, or :memory: to use in-memory database.

int $flags

Optional flags used to determine how to open the SQLite database. By default, open uses SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE.

SQLITE3_OPEN_READONLY: Open the database for reading only.

string $encryptionKey

An optional encryption key used when encrypting and decrypting an SQLite database.

int lastExtendedErrorCode()

Since: 7.4

No description

Return Value

int

bool enableExtendedResultCodes(bool $enable = true)

Since: 7.4

No description

Parameters

bool $enable

Return Value

bool

bool backup(SQLite3 $destination, string $sourceDatabase = 'main', string $destinationDatabase = 'main')

Since: 7.4

No description

Parameters

SQLite3 $destination
string $sourceDatabase
string $destinationDatabase

Return Value

bool

bool setAuthorizer(callable|null $callback)

Since: 8.0

No description

Parameters

callable|null $callback

Return Value

bool