Download the PHP package quasilyte/ksqlite without Composer
On this page you can find all versions of the php package quasilyte/ksqlite. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download quasilyte/ksqlite
More information about quasilyte/ksqlite
Files in quasilyte/ksqlite
Package ksqlite
Short Description FFI-based SQLite library that can be used in both PHP and KPHP
License MIT
Informations about the package ksqlite
KSQLite
KSQLite is a FFI-based SQLite library that can be used in both PHP and KPHP.
Installation
Since this is a FFI library, it needs a dynamic library available during the run time.
Installation steps:
- Install libsqlite3 in your system (if you don't have it already)
- Locate the library file and place in under
./ffilibs/libsqlite3 - Install this composer package to use KSQLite classes inside your code
Depending on your system, you need to find libsqlite3.so, libsqlite3.dylib
or libsqlite3.dll file. Then you can copy it to the application root ffilibs folder
under the libsqlite3 name (note: no extension suffixes).
If you're having difficulties locating the library file, use a helper script:
Then install the composer library itself:
Notes:
- If you want to place library files/links globally, make
./ffilibsa symlink - You'll probably want to add
ffilibs/to your gitignore
Examples
- quick_start.php - a simple overview of the API basics
- prepared_statements.php - how to re-use a single statement for multiple queries
- transactions.php - how to use transactions plus some best practices
- simple_site.php - serving a simple TODO app using SQLite as a database
Running examples with PHP:
Running examples with KPHP:
API reference
All functions report error with false return value (operation status).
When there is more than one result to be returned, a tuple like tuple(T, bool) is returned, where second tuple element is an operation status.
If operation status is false, use KSQLite::getLastError() to get the actual error message.
Note that you only need to care about closing the opened database object. There are no other resources you need to finalize. The API is designed in a way that you don't get any FFI-allocated object, so the library can manage these resources for you.
- exec methods run the query while discarding their results
- fetch methods collect and return results
- query method is a low-level result set iteration privimite; fetch methods are built on that
exec
$sqlSQL query string with optional bind var placeholders$paramsbind variables for the query
When to use: need to execute a query once, but don't need the results.
execPrepared
$sqlSQL query string with optional bind var placeholders$bind_params_funca callback that binds variables for the query
When to use: running a single SQL statement with different params, don't need the results.
If you find prepared statements API too low-level, consider wrapping it in some helper functions.
fetch
$sqlSQL query string with optional bind var placeholders$paramsbind variables for the query$row_funca callback that is called for every row, its return value is collected
If $row_func is null, default mapping behavior is used (rowAssoc).
When to use: execute a query once, collect results.
Notes:
- You can stop the results fetching by using
$ctx->stop() - Use empty array for
$paramsif your query has no bind vars, but you need a custom$row_func
fetchRow
$sqlSQL query string with optional bind var placeholders$paramsbind variables for the query
When to use: execute a query once, collecting exactly one result row.
Note: if query returns more than one row, error will be reported.
Either use LIMIT 1 or other ways to request only 1 row from the database,
or use fetch() method and skip rest of the rows explicitely.
fetchRowAssoc
Like fetchRow, but result array has column name keys instead of indexes.
fetchColumn
$sqlSQL query string with optional bind var placeholders$paramsbind variables for the query
When to use: execute a query once, collecting exactly one result column.
Note: if query returns more than one row or that row contains more than one value, error will be reported.
query
$sqlSQL query string with optional bind var placeholders$paramsbind variables for the query$row_funca void-result callback that is called for every row
Unlike fetch-style API, it does not collect any results on its own. Use external state to do that.
When to use: when query results are needed, but fetch API is not flexible enough.
We're using KSQLiteArray here instead of a normal array since KPHP doesn't support by-reference closure captures.
queryPrepared
$sqlSQL query string with optional bind var placeholders$bind_params_funca callback that binds variables for the query$row_funca void-result callback that is called for every row
When to use: same advantages like with execPrepared, but here you can collect the results.