Skip to content

Commit 5234a51

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 d6d2a71 commit 5234a51

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
@@ -253,7 +259,8 @@ db.aggregate('sumint', {
253259
step: (acc, value) => acc + value,
254260
});
255261

256-
db.prepare('SELECT sumint(y) as total FROM t3').get(); // { total: 21 }
262+
using query = db.prepare('SELECT sumint(y) as total FROM t3');
263+
query.get(); // { total: 21 }
257264
```
258265

259266
```mjs
@@ -274,7 +281,8 @@ db.aggregate('sumint', {
274281
step: (acc, value) => acc + value,
275282
});
276283

277-
db.prepare('SELECT sumint(y) as total FROM t3').get(); // { total: 21 }
284+
using query = db.prepare('SELECT sumint(y) as total FROM t3');
285+
query.get(); // { total: 21 }
278286
```
279287

280288
### `database.close()`
@@ -449,7 +457,8 @@ db.setAuthorizer((actionCode) => {
449457
});
450458

451459
// This will work
452-
db.prepare('SELECT 1').get();
460+
using query = db.prepare('SELECT 1');
461+
query.get();
453462

454463
// This will throw an error due to authorization denial
455464
try {
@@ -472,7 +481,8 @@ db.setAuthorizer((actionCode) => {
472481
});
473482

474483
// This will work
475-
db.prepare('SELECT 1').get();
484+
using query = db.prepare('SELECT 1');
485+
query.get();
476486

477487
// This will throw an error due to authorization denial
478488
try {
@@ -605,7 +615,8 @@ original.close();
605615

606616
const clone = new DatabaseSync(':memory:');
607617
clone.deserialize(buffer);
608-
console.log(clone.prepare('SELECT value FROM t').get());
618+
using query = clone.prepare('SELECT value FROM t');
619+
console.log(query.get());
609620
// Prints: { value: 'hello' }
610621
```
611622

@@ -620,7 +631,8 @@ original.close();
620631

621632
const clone = new DatabaseSync(':memory:');
622633
clone.deserialize(buffer);
623-
console.log(clone.prepare('SELECT value FROM t').get());
634+
using query = clone.prepare('SELECT value FROM t');
635+
console.log(query.get());
624636
// Prints: { value: 'hello' }
625637
```
626638

@@ -677,7 +689,8 @@ sqlTagStore.get`SELECT ${value}`;
677689
is equivalent to:
678690

679691
```js
680-
db.prepare('SELECT ?').get(value);
692+
using statement = db.prepare('SELECT ?');
693+
statement.get(value);
681694
```
682695

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

842855
const session = sourceDb.createSession();
843856

844-
const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
857+
using insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
845858
insert.run(1, 'hello');
846859
insert.run(2, 'world');
847860

@@ -861,7 +874,7 @@ targetDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');
861874

862875
const session = sourceDb.createSession();
863876

864-
const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
877+
using insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
865878
insert.run(1, 'hello');
866879
insert.run(2, 'world');
867880

0 commit comments

Comments
 (0)