Skip to content

Commit 862d994

Browse files
authored
doc: finalize statements in sqlite examples
Signed-off-by: Guilherme Araújo <[email protected]> PR-URL: #65088 Reviewed-By: Aviv Keller <[email protected]> Reviewed-By: René <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 98fd6cb commit 862d994

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

‎doc/api/sqlite.md‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ const insert = database.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
5353
// Execute the prepared statement with bound values.
5454
insert.run(1, 'hello');
5555
insert.run(2, 'world');
56+
// Finalize the prepared statement once it is no longer needed.
57+
insert.close();
5658
// Create a prepared statement to read data from the database.
5759
const query = database.prepare('SELECT * FROM data ORDER BY key');
5860
// Execute the prepared statement and log the result set.
5961
console.log(query.all());
6062
// Prints: [ { key: 1, value: 'hello' }, { key: 2, value: 'world' } ]
63+
query.close();
6164
```
6265

6366
```cjs
@@ -76,11 +79,14 @@ const insert = database.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
7679
// Execute the prepared statement with bound values.
7780
insert.run(1, 'hello');
7881
insert.run(2, 'world');
82+
// Finalize the prepared statement once it is no longer needed.
83+
insert.close();
7984
// Create a prepared statement to read data from the database.
8085
const query = database.prepare('SELECT * FROM data ORDER BY key');
8186
// Execute the prepared statement and log the result set.
8287
console.log(query.all());
8388
// Prints: [ { key: 1, value: 'hello' }, { key: 2, value: 'world' } ]
89+
query.close();
8490
```
8591

8692
## Type conversion between JavaScript and SQLite
@@ -264,7 +270,8 @@ db.aggregate('sumint', {
264270
step: (acc, value) => acc + value,
265271
});
266272

267-
db.prepare('SELECT sumint(y) as total FROM t3').get(); // { total: 21 }
273+
using query = db.prepare('SELECT sumint(y) as total FROM t3');
274+
query.get(); // { total: 21 }
268275
```
269276

270277
```mjs
@@ -285,7 +292,8 @@ db.aggregate('sumint', {
285292
step: (acc, value) => acc + value,
286293
});
287294

288-
db.prepare('SELECT sumint(y) as total FROM t3').get(); // { total: 21 }
295+
using query = db.prepare('SELECT sumint(y) as total FROM t3');
296+
query.get(); // { total: 21 }
289297
```
290298

291299
### `database.close()`
@@ -463,7 +471,8 @@ db.setAuthorizer((actionCode) => {
463471
});
464472

465473
// This will work
466-
db.prepare('SELECT 1').get();
474+
using query = db.prepare('SELECT 1');
475+
query.get();
467476

468477
// This will throw an error due to authorization denial
469478
try {
@@ -486,7 +495,8 @@ db.setAuthorizer((actionCode) => {
486495
});
487496

488497
// This will work
489-
db.prepare('SELECT 1').get();
498+
using query = db.prepare('SELECT 1');
499+
query.get();
490500

491501
// This will throw an error due to authorization denial
492502
try {
@@ -627,7 +637,8 @@ original.close();
627637

628638
const clone = new DatabaseSync(':memory:');
629639
clone.deserialize(buffer);
630-
console.log(clone.prepare('SELECT value FROM t').get());
640+
using query = clone.prepare('SELECT value FROM t');
641+
console.log(query.get());
631642
// Prints: { value: 'hello' }
632643
```
633644

@@ -642,7 +653,8 @@ original.close();
642653

643654
const clone = new DatabaseSync(':memory:');
644655
clone.deserialize(buffer);
645-
console.log(clone.prepare('SELECT value FROM t').get());
656+
using query = clone.prepare('SELECT value FROM t');
657+
console.log(query.get());
646658
// Prints: { value: 'hello' }
647659
```
648660

@@ -699,7 +711,8 @@ sqlTagStore.get`SELECT ${value}`;
699711
is equivalent to:
700712

701713
```js
702-
db.prepare('SELECT ?').get(value);
714+
using statement = db.prepare('SELECT ?');
715+
statement.get(value);
703716
```
704717

705718
However, in the first example, the tag store will cache the underlying prepared
@@ -863,7 +876,7 @@ targetDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');
863876

864877
const session = sourceDb.createSession();
865878

866-
const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
879+
using insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
867880
insert.run(1, 'hello');
868881
insert.run(2, 'world');
869882

@@ -883,7 +896,7 @@ targetDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');
883896

884897
const session = sourceDb.createSession();
885898

886-
const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
899+
using insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
887900
insert.run(1, 'hello');
888901
insert.run(2, 'world');
889902

0 commit comments

Comments
 (0)