Skip to content

Commit 41a39ef

Browse files
araujoguiaduh95
authored andcommitted
doc: finalize statements in sqlite examples
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com> PR-URL: #65088 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: René <contact.9a5d6388@renegade334.me.uk> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9f80e60 commit 41a39ef

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
@@ -51,11 +51,14 @@ const insert = database.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
5151
// Execute the prepared statement with bound values.
5252
insert.run(1, 'hello');
5353
insert.run(2, 'world');
54+
// Finalize the prepared statement once it is no longer needed.
55+
insert.close();
5456
// Create a prepared statement to read data from the database.
5557
const query = database.prepare('SELECT * FROM data ORDER BY key');
5658
// Execute the prepared statement and log the result set.
5759
console.log(query.all());
5860
// Prints: [ { key: 1, value: 'hello' }, { key: 2, value: 'world' } ]
61+
query.close();
5962
```
6063

6164
```cjs
@@ -74,11 +77,14 @@ const insert = database.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
7477
// Execute the prepared statement with bound values.
7578
insert.run(1, 'hello');
7679
insert.run(2, 'world');
80+
// Finalize the prepared statement once it is no longer needed.
81+
insert.close();
7782
// Create a prepared statement to read data from the database.
7883
const query = database.prepare('SELECT * FROM data ORDER BY key');
7984
// Execute the prepared statement and log the result set.
8085
console.log(query.all());
8186
// Prints: [ { key: 1, value: 'hello' }, { key: 2, value: 'world' } ]
87+
query.close();
8288
```
8389

8490
## Type conversion between JavaScript and SQLite
@@ -262,7 +268,8 @@ db.aggregate('sumint', {
262268
step: (acc, value) => acc + value,
263269
});
264270

265-
db.prepare('SELECT sumint(y) as total FROM t3').get(); // { total: 21 }
271+
using query = db.prepare('SELECT sumint(y) as total FROM t3');
272+
query.get(); // { total: 21 }
266273
```
267274

268275
```mjs
@@ -283,7 +290,8 @@ db.aggregate('sumint', {
283290
step: (acc, value) => acc + value,
284291
});
285292

286-
db.prepare('SELECT sumint(y) as total FROM t3').get(); // { total: 21 }
293+
using query = db.prepare('SELECT sumint(y) as total FROM t3');
294+
query.get(); // { total: 21 }
287295
```
288296

289297
### `database.close()`
@@ -461,7 +469,8 @@ db.setAuthorizer((actionCode) => {
461469
});
462470

463471
// This will work
464-
db.prepare('SELECT 1').get();
472+
using query = db.prepare('SELECT 1');
473+
query.get();
465474

466475
// This will throw an error due to authorization denial
467476
try {
@@ -484,7 +493,8 @@ db.setAuthorizer((actionCode) => {
484493
});
485494

486495
// This will work
487-
db.prepare('SELECT 1').get();
496+
using query = db.prepare('SELECT 1');
497+
query.get();
488498

489499
// This will throw an error due to authorization denial
490500
try {
@@ -619,7 +629,8 @@ original.close();
619629

620630
const clone = new DatabaseSync(':memory:');
621631
clone.deserialize(buffer);
622-
console.log(clone.prepare('SELECT value FROM t').get());
632+
using query = clone.prepare('SELECT value FROM t');
633+
console.log(query.get());
623634
// Prints: { value: 'hello' }
624635
```
625636

@@ -634,7 +645,8 @@ original.close();
634645

635646
const clone = new DatabaseSync(':memory:');
636647
clone.deserialize(buffer);
637-
console.log(clone.prepare('SELECT value FROM t').get());
648+
using query = clone.prepare('SELECT value FROM t');
649+
console.log(query.get());
638650
// Prints: { value: 'hello' }
639651
```
640652

@@ -691,7 +703,8 @@ sqlTagStore.get`SELECT ${value}`;
691703
is equivalent to:
692704

693705
```js
694-
db.prepare('SELECT ?').get(value);
706+
using statement = db.prepare('SELECT ?');
707+
statement.get(value);
695708
```
696709

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

856869
const session = sourceDb.createSession();
857870

858-
const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
871+
using insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
859872
insert.run(1, 'hello');
860873
insert.run(2, 'world');
861874

@@ -875,7 +888,7 @@ targetDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');
875888

876889
const session = sourceDb.createSession();
877890

878-
const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
891+
using insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
879892
insert.run(1, 'hello');
880893
insert.run(2, 'world');
881894

0 commit comments

Comments
 (0)