File tree Expand file tree Collapse file tree 4 files changed +21
-15
lines changed Expand file tree Collapse file tree 4 files changed +21
-15
lines changed Original file line number Diff line number Diff line change 1
1
/composer.lock
2
+ /examples /users.db
2
3
/vendor /
Original file line number Diff line number Diff line change @@ -60,30 +60,33 @@ Let's take these projects to the next level together! 🚀
60
60
## Quickstart example
61
61
62
62
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:
65
65
66
66
``` php
67
67
<?php
68
68
69
69
require __DIR__ . '/vendor/autoload.php';
70
70
71
71
$factory = new Clue\React\SQLite\Factory();
72
+ $db = $factory->openLazy(__DIR__ . '/users.db');
72
73
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)');
75
75
76
76
$name = 'Alice';
77
- $db->query('INSERT INTO foo (bar ) VALUES (?)', [$name])->then(
77
+ $db->query('INSERT INTO user (name ) VALUES (?)', [$name])->then(
78
78
function (Clue\React\SQLite\Result $result) use ($name) {
79
79
echo 'New ID for ' . $name . ': ' . $result->insertId . PHP_EOL;
80
+ },
81
+ function (Exception $e) {
82
+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
80
83
}
81
84
);
82
85
83
86
$db->quit();
84
87
```
85
88
86
- See also the [ examples] ( examples ) .
89
+ See also the [ examples] ( examples/ ) .
87
90
88
91
## Usage
89
92
Original file line number Diff line number Diff line change 3
3
require __DIR__ . '/../vendor/autoload.php ' ;
4
4
5
5
$ factory = new Clue \React \SQLite \Factory ();
6
+ $ db = $ factory ->openLazy (__DIR__ . '/users.db ' );
6
7
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
+ } );
12
13
14
+ $ n = isset ($ argv [1 ]) ? $ argv [1 ] : 1 ;
13
15
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 ) {
15
17
echo 'New row ' . $ result ->insertId . PHP_EOL ;
16
18
}, function (Exception $ e ) {
17
19
echo 'Error: ' . $ e ->getMessage () . PHP_EOL ;
Original file line number Diff line number Diff line change 3
3
require __DIR__ . '/../vendor/autoload.php ' ;
4
4
5
5
$ factory = new Clue \React \SQLite \Factory ();
6
+ $ db = $ factory ->openLazy (__DIR__ . '/users.db ' );
6
7
7
- $ search = isset ($ argv [1 ]) ? $ argv [1 ] : 'foo ' ;
8
- $ db = $ factory ->openLazy ('test.db ' );
8
+ $ search = isset ($ argv [1 ]) ? $ argv [1 ] : '' ;
9
9
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 ) {
11
11
echo 'Found ' . count ($ result ->rows ) . ' rows: ' . PHP_EOL ;
12
12
echo implode ("\t" , $ result ->columns ) . PHP_EOL ;
13
13
foreach ($ result ->rows as $ row ) {
You can’t perform that action at this time.
0 commit comments