https://github.com/thipages/sqlitecli
SQLite CLI PHP wrapper
https://github.com/thipages/sqlitecli
cli php sqlite wrapper
Last synced: about 1 month ago
JSON representation
SQLite CLI PHP wrapper
- Host: GitHub
- URL: https://github.com/thipages/sqlitecli
- Owner: thipages
- License: mit
- Created: 2020-07-26T15:57:55.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-08-09T23:17:30.000Z (almost 5 years ago)
- Last Synced: 2025-03-09T08:44:18.723Z (over 1 year ago)
- Topics: cli, php, sqlite, wrapper
- Language: PHP
- Homepage:
- Size: 54.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqlitecli
SQLite CLI wrapper
## Installation
**composer** require thipages\sqlitecli
## Usage
```php
/* index.php */
/* ********* */
$cli=new SqliteCli('./database.db');
$cli->execute(
"CREATE TABLE simple (id INTEGER PRIMARY KEY, name);",
"INSERT INTO simple (name) VALUES ('Paul'), ('Jack'),('Charlie');",
'.mode csv',
'.headers on',
'.separator ,',
'.output data.csv',
'select id,name from simple limit 2;'
);
// OR
$cli-execute(
"CREATE TABLE simple (id INTEGER PRIMARY KEY, name);",
"INSERT INTO simple (name) VALUES ('Paul'), ('Jack'),('Charlie');",
"SELECT name FROM simple WHERE id=1;",
function($res) {
// Set 'Paul' to all records
return "UPDATE simple SET name='$res'";
}
);
```
- with functions and registry
```php
/* index.php */
/* ********* */
$cli=new SqliteCli('./database.db');
$cli->execute(
"CREATE TABLE simple (id INTEGER PRIMARY KEY, name);",
"INSERT INTO simple (name) VALUES ('Paul'), ('Jack'),('Charlie');",
"SELECT name FROM simple WHERE id=1;",
Orders::registerAs('QUERY_ID=1'),
function($res, $registry) {
$newName=$registry->get(''QUERY_ID=1'');
return "UPDATE simple SET name='$newName'";
}
);
```
Same result for both.
## API
**SqliteCli class**
###### Constructor
`SqliteCli($dbPath)`
###### Methods
`execute(...$orders):[boolean,array]` executes sqlite commands (list of [array of] commands). This method adds a final `.quit` command
`getRegistry($key=null)` get the registry ouput value of `$key` or if null the registry as an associated array.
**Orders class**
###### Static methods
`addPrimary($table,$primaryName):[boolean,array]` adds a primary field to an existing table (first position)
`addField($table,$definition):[boolean,array]` adds a field to an existing table (first position)
`importCsv($table, $csvPath, $separator=',', $headers='on'):array` returns an array of commands for csv import
`exportCsv($csvPath, $separator=',', $headers='on'):array` returns an array of commands for csv export
`mergeCsvList($table,$csvPaths, $delimiter=',')` merges csv Files into `$table`. Files need to have the same fields. `$delimiter` can be an array matching `$csvPaths`
`getFieldList($table)` returns an array of the fields of `$table`
`registerAs($key)` associates the previous output command to `$key` in the registry
`unregister(...$keys)` associates the previous output command to `$key` in the registry