Skip to content

Commit a57b936

Browse files
committed
Improve examples
1 parent 34d55c3 commit a57b936

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/composer.lock
2+
/examples/users.db
23
/vendor/

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,33 @@ Let's take these projects to the next level together! 🚀
6060
## Quickstart example
6161

6262
The following example code demonstrates how this library can be used to open an
63-
existing SQLite database file (or automatically create it on first run) and then
64-
`INSERT` a new record to the database:
63+
existing SQLite database file (or automatically create it on the first run) and
64+
then `INSERT` a new record to the database:
6565

6666
```php
6767
<?php
6868

6969
require __DIR__ . '/vendor/autoload.php';
7070

7171
$factory = new Clue\React\SQLite\Factory();
72+
$db = $factory->openLazy(__DIR__ . '/users.db');
7273

73-
$db = $factory->openLazy('users.db');
74-
$db->exec('CREATE TABLE IF NOT EXISTS foo (id INTEGER PRIMARY KEY AUTOINCREMENT, bar STRING)');
74+
$db->exec('CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING)');
7575

7676
$name = 'Alice';
77-
$db->query('INSERT INTO foo (bar) VALUES (?)', [$name])->then(
77+
$db->query('INSERT INTO user (name) VALUES (?)', [$name])->then(
7878
function (Clue\React\SQLite\Result $result) use ($name) {
7979
echo 'New ID for ' . $name . ': ' . $result->insertId . PHP_EOL;
80+
},
81+
function (Exception $e) {
82+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
8083
}
8184
);
8285

8386
$db->quit();
8487
```
8588

86-
See also the [examples](examples).
89+
See also the [examples](examples/).
8790

8891
## Usage
8992

examples/insert.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
require __DIR__ . '/../vendor/autoload.php';
44

55
$factory = new Clue\React\SQLite\Factory();
6+
$db = $factory->openLazy(__DIR__ . '/users.db');
67

7-
$n = isset($argv[1]) ? $argv[1] : 1;
8-
$db = $factory->openLazy('test.db');
9-
10-
$promise = $db->exec('CREATE TABLE IF NOT EXISTS foo (id INTEGER PRIMARY KEY AUTOINCREMENT, bar STRING)');
11-
$promise->then(null, 'printf');
8+
$db->exec(
9+
'CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING)'
10+
)->then(null, function (Exception $e) {
11+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
12+
});
1213

14+
$n = isset($argv[1]) ? $argv[1] : 1;
1315
for ($i = 0; $i < $n; ++$i) {
14-
$db->exec("INSERT INTO foo (bar) VALUES ('This is a test')")->then(function (Clue\React\SQLite\Result $result) {
16+
$db->exec("INSERT INTO user (name) VALUES ('Alice')")->then(function (Clue\React\SQLite\Result $result) {
1517
echo 'New row ' . $result->insertId . PHP_EOL;
1618
}, function (Exception $e) {
1719
echo 'Error: ' . $e->getMessage() . PHP_EOL;

examples/search.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
require __DIR__ . '/../vendor/autoload.php';
44

55
$factory = new Clue\React\SQLite\Factory();
6+
$db = $factory->openLazy(__DIR__ . '/users.db');
67

7-
$search = isset($argv[1]) ? $argv[1] : 'foo';
8-
$db = $factory->openLazy('test.db');
8+
$search = isset($argv[1]) ? $argv[1] : '';
99

10-
$db->query('SELECT * FROM foo WHERE bar LIKE ?', ['%' . $search . '%'])->then(function (Clue\React\SQLite\Result $result) {
10+
$db->query('SELECT * FROM user WHERE name LIKE ?', ['%' . $search . '%'])->then(function (Clue\React\SQLite\Result $result) {
1111
echo 'Found ' . count($result->rows) . ' rows: ' . PHP_EOL;
1212
echo implode("\t", $result->columns) . PHP_EOL;
1313
foreach ($result->rows as $row) {

0 commit comments

Comments
 (0)