【发布时间】:2020-08-15 08:51:24
【问题描述】:
我创建了一个 AWS Lambda 函数来从 AWS RDS 数据库中检索信息。我还在 AWS API Gateway 中创建了一个触发 Lambda 函数的 API。当我的 SQL 语句是“select * from user”时,API 工作正常。但是,当我尝试“从带有标签 = 人的用户中选择 *”之类的操作时,我收到此错误:
{"errorType":"Error","errorMessage":"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 'tag = 'people'' at line 1","trace":["Error: 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 'tag = 'people'' at line 1"," at PromisePool.query (/var/task/node_modules/mysql2/promise.js:330:22)"," at Runtime.module.exports.handler (/var/task/index.js:19:38)"," at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]}
我的 AWS Lambda 函数定义为 (Node.js 12.x):
const mysql = require('mysql2');
const pool = mysql.createPool({
host: process.env.LAMBDA_HOSTNAME,
user: process.env.LAMBDA_USERNAME,
password: process.env.LAMBDA_PASSWORD,
database: process.env.LAMBDA_DATABASE,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
module.exports.handler = async (event, context, callback) => {
context.callbackWaitsForEmptyLoop = false;
const tag = event["params"]["querystring"]["tag"]
const sql = "select * from user with tag = " + tag;
const promisePool = pool.promise();
const [rows] = await promisePool.query(sql);
const lambdaProxyResponse = {
statusCode: 200,
body: JSON.stringify([rows])
};
return lambdaProxyResponse;
};
有人可以帮我解决这个问题吗?另外,当我在 mysql workbench 中运行命令时,它也能正确执行。
【问题讨论】:
标签: javascript mysql aws-lambda aws-api-gateway amazon-rds