mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
doc: finalize statements in sqlite examples
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com> PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
1 parent
5cb78ed300
commit
fa52f11d3b
1 file changed
+22
-9
+22
-9
@@ -51,11 +51,14 @@ const insert = database.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
|
||||
// Execute the prepared statement with bound values.
|
||||
insert.run(1, 'hello');
|
||||
insert.run(2, 'world');
|
||||
// Finalize the prepared statement once it is no longer needed.
|
||||
insert.close();
|
||||
// Create a prepared statement to read data from the database.
|
||||
const query = database.prepare('SELECT * FROM data ORDER BY key');
|
||||
// Execute the prepared statement and log the result set.
|
||||
console.log(query.all());
|
||||
// Prints: [ { key: 1, value: 'hello' }, { key: 2, value: 'world' } ]
|
||||
query.close();
|
||||
```
|
||||
|
||||
```cjs
|
||||
@@ -74,11 +77,14 @@ const insert = database.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
|
||||
// Execute the prepared statement with bound values.
|
||||
insert.run(1, 'hello');
|
||||
insert.run(2, 'world');
|
||||
// Finalize the prepared statement once it is no longer needed.
|
||||
insert.close();
|
||||
// Create a prepared statement to read data from the database.
|
||||
const query = database.prepare('SELECT * FROM data ORDER BY key');
|
||||
// Execute the prepared statement and log the result set.
|
||||
console.log(query.all());
|
||||
// Prints: [ { key: 1, value: 'hello' }, { key: 2, value: 'world' } ]
|
||||
query.close();
|
||||
```
|
||||
|
||||
## Type conversion between JavaScript and SQLite
|
||||
@@ -262,7 +268,8 @@ db.aggregate('sumint', {
|
||||
step: (acc, value) => acc + value,
|
||||
});
|
||||
|
||||
db.prepare('SELECT sumint(y) as total FROM t3').get(); // { total: 21 }
|
||||
using query = db.prepare('SELECT sumint(y) as total FROM t3');
|
||||
query.get(); // { total: 21 }
|
||||
```
|
||||
|
||||
```mjs
|
||||
@@ -283,7 +290,8 @@ db.aggregate('sumint', {
|
||||
step: (acc, value) => acc + value,
|
||||
});
|
||||
|
||||
db.prepare('SELECT sumint(y) as total FROM t3').get(); // { total: 21 }
|
||||
using query = db.prepare('SELECT sumint(y) as total FROM t3');
|
||||
query.get(); // { total: 21 }
|
||||
```
|
||||
|
||||
### `database.close()`
|
||||
@@ -461,7 +469,8 @@ db.setAuthorizer((actionCode) => {
|
||||
});
|
||||
|
||||
// This will work
|
||||
db.prepare('SELECT 1').get();
|
||||
using query = db.prepare('SELECT 1');
|
||||
query.get();
|
||||
|
||||
// This will throw an error due to authorization denial
|
||||
try {
|
||||
@@ -484,7 +493,8 @@ db.setAuthorizer((actionCode) => {
|
||||
});
|
||||
|
||||
// This will work
|
||||
db.prepare('SELECT 1').get();
|
||||
using query = db.prepare('SELECT 1');
|
||||
query.get();
|
||||
|
||||
// This will throw an error due to authorization denial
|
||||
try {
|
||||
@@ -619,7 +629,8 @@ original.close();
|
||||
|
||||
const clone = new DatabaseSync(':memory:');
|
||||
clone.deserialize(buffer);
|
||||
console.log(clone.prepare('SELECT value FROM t').get());
|
||||
using query = clone.prepare('SELECT value FROM t');
|
||||
console.log(query.get());
|
||||
// Prints: { value: 'hello' }
|
||||
```
|
||||
|
||||
@@ -634,7 +645,8 @@ original.close();
|
||||
|
||||
const clone = new DatabaseSync(':memory:');
|
||||
clone.deserialize(buffer);
|
||||
console.log(clone.prepare('SELECT value FROM t').get());
|
||||
using query = clone.prepare('SELECT value FROM t');
|
||||
console.log(query.get());
|
||||
// Prints: { value: 'hello' }
|
||||
```
|
||||
|
||||
@@ -691,7 +703,8 @@ sqlTagStore.get`SELECT ${value}`;
|
||||
is equivalent to:
|
||||
|
||||
```js
|
||||
db.prepare('SELECT ?').get(value);
|
||||
using statement = db.prepare('SELECT ?');
|
||||
statement.get(value);
|
||||
```
|
||||
|
||||
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)');
|
||||
|
||||
const session = sourceDb.createSession();
|
||||
|
||||
const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
|
||||
using insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
|
||||
insert.run(1, 'hello');
|
||||
insert.run(2, 'world');
|
||||
|
||||
@@ -875,7 +888,7 @@ targetDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');
|
||||
|
||||
const session = sourceDb.createSession();
|
||||
|
||||
const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
|
||||
using insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
|
||||
insert.run(1, 'hello');
|
||||
insert.run(2, 'world');
|
||||
|
||||
|
||||
Reference in new issue
Block a user