| 321 | } |
| 322 | |
| 323 | async function queryDataWithParameter(instanceId, databaseId, projectId) { |
| 324 | // [START spanner_query_with_parameter] |
| 325 | // Imports the Google Cloud client library |
| 326 | const {Spanner} = require('@google-cloud/spanner'); |
| 327 | |
| 328 | /** |
| 329 | * TODO(developer): Uncomment the following lines before running the sample. |
| 330 | */ |
| 331 | // const projectId = 'my-project-id'; |
| 332 | // const instanceId = 'my-instance'; |
| 333 | // const databaseId = 'my-database'; |
| 334 | |
| 335 | // Creates a client |
| 336 | const spanner = new Spanner({ |
| 337 | projectId: projectId, |
| 338 | }); |
| 339 | |
| 340 | // Gets a reference to a Cloud Spanner instance and database |
| 341 | const instance = spanner.instance(instanceId); |
| 342 | const database = instance.database(databaseId); |
| 343 | |
| 344 | const query = { |
| 345 | sql: `SELECT SingerId, FirstName, LastName |
| 346 | FROM Singers WHERE LastName = @lastName`, |
| 347 | params: { |
| 348 | lastName: 'Garcia', |
| 349 | }, |
| 350 | }; |
| 351 | |
| 352 | // Queries rows from the Albums table |
| 353 | try { |
| 354 | const [rows] = await database.run(query); |
| 355 | |
| 356 | rows.forEach(row => { |
| 357 | const json = row.toJSON(); |
| 358 | console.log( |
| 359 | `SingerId: ${json.SingerId}, FirstName: ${json.FirstName}, LastName: ${json.LastName}` |
| 360 | ); |
| 361 | }); |
| 362 | } catch (err) { |
| 363 | console.error('ERROR:', err); |
| 364 | } finally { |
| 365 | // Close the database when finished. |
| 366 | await database.close(); |
| 367 | } |
| 368 | // [END spanner_query_with_parameter] |
| 369 | } |
| 370 | |
| 371 | async function writeWithTransactionUsingDml(instanceId, databaseId, projectId) { |
| 372 | // [START spanner_dml_getting_started_update] |