[英]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. 由于明显的原因,省略了
connectionName
, dbUser
, dbPassword
和dbName
。
{"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 guest
,I 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 guest
,I got here!
); INSERT INTO项(guestName,content)值(second guest
,Me 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));
}
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.