@@ -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
@@ -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
455464try {
@@ -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
478488try {
@@ -605,7 +615,8 @@ original.close();
605615
606616const clone = new DatabaseSync (' :memory:' );
607617clone .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
621632const clone = new DatabaseSync (' :memory:' );
622633clone .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}`;
677689is equivalent to:
678690
679691``` js
680- db .prepare (' SELECT ?' ).get (value);
692+ using statement = db .prepare (' SELECT ?' );
693+ statement .get (value);
681694```
682695
683696However, 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
842855const 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 (?, ?)' );
845858insert .run (1 , ' hello' );
846859insert .run (2 , ' world' );
847860
@@ -861,7 +874,7 @@ targetDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');
861874
862875const 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 (?, ?)' );
865878insert .run (1 , ' hello' );
866879insert .run (2 , ' world' );
867880
0 commit comments