Skip to content

Commit a5ef016

Browse files
committed
doc: finalize statements in sqlite examples
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
1 parent 3d80990 commit a5ef016

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

‎doc/api/sqlite.md‎

Lines changed: 20 additions & 8 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

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

864876
const session = sourceDb.createSession();
865877

866-
const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
878+
using insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
867879
insert.run(1, 'hello');
868880
insert.run(2, 'world');
869881

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

884896
const session = sourceDb.createSession();
885897

886-
const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
898+
using insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
887899
insert.run(1, 'hello');
888900
insert.run(2, 'world');
889901

0 commit comments

Comments
 (0)