Download the PHP package alexoliverwd/basic-sqlite without Composer
On this page you can find all versions of the php package alexoliverwd/basic-sqlite. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alexoliverwd/basic-sqlite
More information about alexoliverwd/basic-sqlite
Files in alexoliverwd/basic-sqlite
Package basic-sqlite
Short Description A simple SQLite helper that makes executing queries easier.
License MIT
Informations about the package basic-sqlite
Basic SQLite
Basic SQLite is a lightweight PHP helper class designed to simplify interaction with SQLite databases. It streamlines the process of connecting to, querying, and managing SQLite databases.
Installation
Preferred installation is via Composer:
Basic Usage
The constructor initializes a new instance of the class, setting up the connection to the specified SQLite database file. It performs validation to ensure the provided path points to a valid directory and constructs the full path to the SQLite file.
If the parsed directory exists but the specified SQLite file does not, the file will be created automatically.
When establishing a new class instance, the below methods are available:
- registerColumn
- beginWriteTransaction
- close
- completeWriteTransaction
- getColumns
- getCurrentTableName
- getDatabaseLocation
- getIndices
- getKeyFromName
- getNames
- hasColumn
- migrate
- query
- queryIsWriteStatement
- setTableName
TLDR
In summary, the example below demonstrates how to use the SQLite helper class to:
- Set up a database connection.
- Define a table schema.
- Insert records using prepared statements.
- Query the database.
- Close the connection.
Public Methods
The below methods are detailed descriptions, parameters, return types, and usage examples for each method.
__construct
Description
Initializes the SQLite class with a specified database location and optional pragmas.
Parameters
$db_location
: The file path to the SQLite database.$pragmas
: An optional associative array of pragmas to configure the SQLite database.
Pragma Defaults
By default, the following pragmas are set when initializing a new instance:
- journal_mode: WAL
- busy_timeout: 5000
- synchronous: NORMAL
- cache_size: 2000
- temp_store: memory
- foreign_keys: true
Returns
An instance of the SQLite class.
Example
registerColumn
Description
Is used to define a new column in the database table managed by the SQLite
class. It supports various column attributes, such as type, nullability, and indexing.
Parameters
$column_name
(string): The name of the column to be added.$type
(DataType): The data type of the column (e.g.,TEXT
,INTEGER
).$can_be_null
(bool) (default:true
): Specifies whether the column can acceptNULL
values.$is_post_required
(bool) (default:true
): Indicates whether the column must be included in a POST request.$is_index
(bool) (default:false
): Determines if the column should be indexed for faster lookups.$is_unique
(bool) (default:false
): Specifies whether the column should enforce unique constraints.
Return Value
This method does not return a value.
Example Usage
beginWriteTransaction
Description
Starts a write transaction for the SQLite database.
Returns
Void.
Example
close
Description
Closes the connection to the SQLite database.
Returns
Void.
Example
completeWriteTransaction
Description
Completes the current write transaction, committing any changes to the database.
Returns
Void.
Example
getColumns
Description
Retrieves a list of column names for the current table.
Returns
Array of column names.
Example
getCurrentTableName
Description
Retrieves the name of the current table being operated on.
Returns
The name of the current table as a string.
Example
getDatabaseLocation
Description
Retrieves the file path of the SQLite database.
Returns
The database file path as a string.
Example
getIndices
Description
Retrieves a list of indices for the current table.
Returns
Array of index names.
Example
getKeyFromName
Description
Finds the key corresponding to a given name in an array of items.
Parameters
$items
: An array of items.$name
: The name to find the key for.
Returns
The key corresponding to the given name.
Example
getNames
Description
Extracts names from an array of items.
Parameters
$items
: An array of items to extract names from.
Returns
Array of names.
Example
hasColumn
Description
Checks whether a specified column exists in the current table.
Parameters
$column_name
: The name of the column to check.
Returns
Boolean indicating whether the column exists.
Example
migrate
Description
Performs migrations to ensure the database schema is up-to-date.
Returns
Void.
Example
query
Description
Executes an SQL query on the database, with optional parameter binding and row return.
Parameters
$query
: The SQL query string to execute.$return_rows
: Whether to return rows from the query. Defaults to true.$bind_params
: An associative array of parameters to bind to the query.
Returns
Array of rows if $return_rows
is true; otherwise, Void.
Example
queryIsWriteStatement
Description
Checks if a given query is a write operation (e.g., INSERT, UPDATE, DELETE).
Parameters
$query
: The SQL query string to analyze.
Returns
Boolean indicating whether the query is a write operation.
Example
setTableName
Description
Sets the name of the table to be used for subsequent operations.
Parameters
$table_name
: The name of the table.
Returns
Void.