MySQL:mysql 連線

使用 Node.js mysql 模組,開啟及關閉與 MySQL 適用的 Cloud SQL 的連線,執行 SQL INSERT 陳述式。

深入探索

如需包含此程式碼範例的詳細說明文件,請參閱以下文件:

程式碼範例

Node.js

如要驗證 MySQL 適用的 Cloud SQL,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。

try {
  const stmt = 'INSERT INTO votes (time_cast, candidate) VALUES (?, ?)';
  // Pool.query automatically checks out, uses, and releases a connection
  // back into the pool, ensuring it is always returned successfully.
  await pool.query(stmt, [timestamp, team]);
} catch (err) {
  // If something goes wrong, handle the error in this section. This might
  // involve retrying or adjusting parameters depending on the situation.
  // ...
}

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱 Google Cloud 範例瀏覽器