简体   繁体   English

从Node.js Cloud Function插入Google Cloud SQL

[英]Insert into Google Cloud SQL from Node.js Cloud Function

I have a Google Cloud SQL instance running MySQL Second Generation, and a Node.js Cloud Function. 我有一个运行MySQL第二代的Google Cloud SQL实例,以及一个Node.js Cloud Function。

index.js (below) produces the following error when run. index.js(以下)在运行时产生以下错误。 How can a Google Cloud SQL database insert be performed from a Node.js environment. 如何从Node.js环境执行Google Cloud SQL数据库插入。 From what I understand my syntax is correct, but I must be missing something. 据我了解,我的语法是正确的,但是我一定缺少一些东西。 connectionName , dbUser , dbPassword , and dbName have been omitted for obvious reasons. 由于明显的原因,省略了connectionNamedbUserdbPassworddbName

{"code":"ER_PARSE_ERROR","errno":1064,"sqlMessage":"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO entries (guestName, content) values ( first guest , I got here! );I' at line 1","sqlState":"42000","index":0,"sql":"CREATE TABLE entries (guestName VARCHAR(255), content VARCHAR(255),entryID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(entryID));INSERT INTO entries (guestName, content) values ( first guest , I got here! );INSERT INTO entries (guestName, content) values ( second guest , Me too! );"} {“ code”:“ ER_PARSE_ERROR”,“ errno”:1064,“ sqlMessage”:“”您的SQL语法有误;请查看与您的MySQL服务器版本相对应的手册,以在'INSERT INTO条目附近使用正确的语法(guestName,内容)值( first guestI got here! );我在第1行“,” sqlState“:” 42000“,” index“:0,” sql“:” CREATE TABLE条目(guestName VARCHAR(255 ),内容VARCHAR(255),entryID INT不能为空AUTO_INCREMENT,PRIMARY KEY(entryID)); INSERT INTO项(guestName,content)值( first guestI got here! ); INSERT INTO项(guestName,content)值( second guestMe too! );“}

index.js index.js

const mysql = require('mysql');

const connectionName = process.env.INSTANCE_CONNECTION_NAME || '';
const dbUser = process.env.SQL_USER || '';
const dbPassword = process.env.SQL_PASSWORD || '';
const dbName = process.env.SQL_NAME || '';

const mysqlConfig = {
  connectionLimit: 1,
  user: dbUser,
  password: dbPassword,
  database: dbName
};

if (process.env.NODE_ENV === 'production') {
  mysqlConfig.socketPath = `/cloudsql/${connectionName}`;
}

let mysqlPool;

exports.populateDatabase = (req, res) => {
  if (!mysqlPool) {
    mysqlPool = mysql.createPool(mysqlConfig);
  }

  mysqlPool.query('CREATE TABLE entries (guestName VARCHAR(255), content VARCHAR(255),' +
                  'entryID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(entryID));' + 
                  'INSERT INTO entries (guestName, content) values (`first guest`, `I got here!`);' + 
                  'INSERT INTO entries (guestName, content) values (`second guest`, `Me too!`);', (err, results) => {
    if (err) {
      console.error(err);
      res.status(500).send(err);
    } else {
      res.send(JSON.stringify(results));
    }
  });

};

You're passing several statements to your .query() method call. 您正在将多个语句传递给您的.query()方法调用。 By default, multiple statements aren't permitted. 默认情况下,不允许使用多个语句。

Multiple statement queries can be enabled , but be aware that this poses a potential security risk. 可以启用多个语句查询,但是请注意,这会带来潜在的安全风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 GKE pod 查询 Node.js 中的 Google 云 SQL 实例,云 sql 代理作为边车运行 - Query a Google cloud SQL instance in Node.js from a GKE pod with cloud sql proxy running as sidecar 无法使用node.js连接到Google Cloud SQL服务 - Unable to connect to google cloud sql service using node.js node.js 中 Google Cloud mySQL 的连接字符串是什么? - What is the connection string for Google Cloud mySQL, in node.js? Google Cloud SQL - 缓慢的INSERT性能 - Google Cloud SQL - slow INSERT performance 插入性能慢谷歌云 SQL - Slow insert performance google cloud SQL 使用Node.js连接到Google Cloud上的MySQL数据库(错误:连接ENOENT) - Connecting to MySQL database on Google Cloud with Node.js (Error: connect ENOENT) 将项目启动到云/生产(Node.js + Vue.js) - Launching project to cloud/production (Node.js + Vue.js) Bluemix Cloud Platform:如何在node.js MySQL中传递值 - Bluemix Cloud Platform: How to pass a value in node.js MySQL Google Cloud Function:删除 firestore 文档并插入 SQL 查询的结果导致超时 - Google Cloud Function : Delete firestore docs and insert the result of a SQL Query leads to timeout 从 WaveMaker Online 连接到 Google Cloud SQL - Connecting to Google Cloud SQL from WaveMaker Online
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM