@@ -51,11 +51,14 @@ const insert = database.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
5151// Execute the prepared statement with bound values.
5252insert .run (1 , ' hello' );
5353insert .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.
5557const query = database .prepare (' SELECT * FROM data ORDER BY key' );
5658// Execute the prepared statement and log the result set.
5759console .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.
7578insert .run (1 , ' hello' );
7679insert .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.
7883const query = database .prepare (' SELECT * FROM data ORDER BY key' );
7984// Execute the prepared statement and log the result set.
8085console .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
467476try {
@@ -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
490500try {
@@ -619,7 +629,8 @@ original.close();
619629
620630const clone = new DatabaseSync (' :memory:' );
621631clone .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
635646const clone = new DatabaseSync (' :memory:' );
636647clone .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}`;
691703is equivalent to:
692704
693705``` js
694- db .prepare (' SELECT ?' ).get (value);
706+ using statement = db .prepare (' SELECT ?' );
707+ statement .get (value);
695708```
696709
697710However, 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
856869const 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 (?, ?)' );
859872insert .run (1 , ' hello' );
860873insert .run (2 , ' world' );
861874
@@ -875,7 +888,7 @@ targetDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');
875888
876889const 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 (?, ?)' );
879892insert .run (1 , ' hello' );
880893insert .run (2 , ' world' );
881894
0 commit comments