| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5cb78ed commit fa52f11
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,11 +51,14 @@ const insert = database.prepare('INSERT INTO data (key, value) VALUES (?, ?)'); | |||
| 51 | 51 | // Execute the prepared statement with bound values. | |
| 52 | 52 | insert.run(1, 'hello'); | |
| 53 | 53 | insert.run(2, 'world'); | |
| 54 | + // Finalize the prepared statement once it is no longer needed. | ||
| 55 | + insert.close(); | ||
| 54 | 56 | // Create a prepared statement to read data from the database. | |
| 55 | 57 | const query = database.prepare('SELECT * FROM data ORDER BY key'); | |
| 56 | 58 | // Execute the prepared statement and log the result set. | |
| 57 | 59 | console.log(query.all()); | |
| 58 | 60 | // Prints: [ { key: 1, value: 'hello' }, { key: 2, value: 'world' } ] | |
| 61 | + query.close(); | ||
| 59 | 62 | ``` | |
| 60 | 63 | ||
| 61 | 64 | ```cjs | |
@@ -74,11 +77,14 @@ const insert = database.prepare('INSERT INTO data (key, value) VALUES (?, ?)'); | |||
| 74 | 77 | // Execute the prepared statement with bound values. | |
| 75 | 78 | insert.run(1, 'hello'); | |
| 76 | 79 | insert.run(2, 'world'); | |
| 80 | + // Finalize the prepared statement once it is no longer needed. | ||
| 81 | + insert.close(); | ||
| 77 | 82 | // Create a prepared statement to read data from the database. | |
| 78 | 83 | const query = database.prepare('SELECT * FROM data ORDER BY key'); | |
| 79 | 84 | // Execute the prepared statement and log the result set. | |
| 80 | 85 | console.log(query.all()); | |
| 81 | 86 | // Prints: [ { key: 1, value: 'hello' }, { key: 2, value: 'world' } ] | |
| 87 | + query.close(); | ||
| 82 | 88 | ``` | |
| 83 | 89 | ||
| 84 | 90 | ## Type conversion between JavaScript and SQLite | |
@@ -262,7 +268,8 @@ db.aggregate('sumint', { | |||
| 262 | 268 | step: (acc, value) => acc + value, | |
| 263 | 269 | }); | |
| 264 | 270 | ||
| 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 } | ||
| 266 | 273 | ``` | |
| 267 | 274 | ||
| 268 | 275 | ```mjs | |
@@ -283,7 +290,8 @@ db.aggregate('sumint', { | |||
| 283 | 290 | step: (acc, value) => acc + value, | |
| 284 | 291 | }); | |
| 285 | 292 | ||
| 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 } | ||
| 287 | 295 | ``` | |
| 288 | 296 | ||
| 289 | 297 | ### `database.close()` | |
@@ -461,7 +469,8 @@ db.setAuthorizer((actionCode) => { | |||
| 461 | 469 | }); | |
| 462 | 470 | ||
| 463 | 471 | // This will work | |
| 464 | - db.prepare('SELECT 1').get(); | ||
| 472 | + using query = db.prepare('SELECT 1'); | ||
| 473 | + query.get(); | ||
| 465 | 474 | ||
| 466 | 475 | // This will throw an error due to authorization denial | |
| 467 | 476 | try { | |
@@ -484,7 +493,8 @@ db.setAuthorizer((actionCode) => { | |||
| 484 | 493 | }); | |
| 485 | 494 | ||
| 486 | 495 | // This will work | |
| 487 | - db.prepare('SELECT 1').get(); | ||
| 496 | + using query = db.prepare('SELECT 1'); | ||
| 497 | + query.get(); | ||
| 488 | 498 | ||
| 489 | 499 | // This will throw an error due to authorization denial | |
| 490 | 500 | try { | |
@@ -619,7 +629,8 @@ original.close(); | |||
| 619 | 629 | ||
| 620 | 630 | const clone = new DatabaseSync(':memory:'); | |
| 621 | 631 | clone.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()); | ||
| 623 | 634 | // Prints: { value: 'hello' } | |
| 624 | 635 | ``` | |
| 625 | 636 | ||
@@ -634,7 +645,8 @@ original.close(); | |||
| 634 | 645 | ||
| 635 | 646 | const clone = new DatabaseSync(':memory:'); | |
| 636 | 647 | clone.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()); | ||
| 638 | 650 | // Prints: { value: 'hello' } | |
| 639 | 651 | ``` | |
| 640 | 652 | ||
@@ -691,7 +703,8 @@ sqlTagStore.get`SELECT ${value}`; | |||
| 691 | 703 | is equivalent to: | |
| 692 | 704 | ||
| 693 | 705 | ```js | |
| 694 | - db.prepare('SELECT ?').get(value); | ||
| 706 | + using statement = db.prepare('SELECT ?'); | ||
| 707 | + statement.get(value); | ||
| 695 | 708 | ``` | |
| 696 | 709 | ||
| 697 | 710 | 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)'); | |||
| 855 | 868 | ||
| 856 | 869 | const session = sourceDb.createSession(); | |
| 857 | 870 | ||
| 858 | - const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)'); | ||
| 871 | + using insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)'); | ||
| 859 | 872 | insert.run(1, 'hello'); | |
| 860 | 873 | insert.run(2, 'world'); | |
| 861 | 874 | ||
@@ -875,7 +888,7 @@ targetDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)'); | |||
| 875 | 888 | ||
| 876 | 889 | const session = sourceDb.createSession(); | |
| 877 | 890 | ||
| 878 | - const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)'); | ||
| 891 | + using insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)'); | ||
| 879 | 892 | insert.run(1, 'hello'); | |
| 880 | 893 | insert.run(2, 'world'); | |
| 881 | 894 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments