我需要将 json 文件中的数据加载到名为 Locality 的 MySQL 数据库表中。下面的代码返回 MySQL 语法错误。适用于 Aurora MySQL Serverless 的 MySQL 版本 5.7.30。我正在使用 aws-sdk,导入所有库,创建 RDS 实例。但由于某种原因,它返回 SQL 语法错误。
如果我能看到 rds.batchExecuteStatement 作为 SQL 生成的内容,这也会有所帮助。我已经尝试过,但无法找到 SQL 语句是什么。(帮助?)
我已将以下 AWS 文档 Using the Data API for RDS Serverless 中的 python 翻译为 javascript
查看 batchExecuteStatement 函数的快速链接: RDS.batchExecuteStatement Docs
const loadLocalities = () => {
// Read data from the json file
const data = require('./GeoDyDB.json')
// Extract the data from the JSON into a format RDSDataService.batchExecuteStatement expects for the parameterSets argument
const localityParameterSets = data.map(function(location){
return [
{name: 'code', value: {stringValue: generate_locality_sort_key(location)}},
{name: 'synonyms', value: {stringValue: location.formatted_address}},
{name: 'country', value: {stringValue: location.Country}},
{name: 'state', value: {stringValue: location.State}},
{name: 'city', value: {stringValue: location.City}},
{name: 'zone', value: {stringValue: location.Zone}},
{name: 'ward', value: {stringValue: location.Ward}},
{name: 'colony', value: {stringValue: location.Colony}},
{name: 'pincode', value: {isNull: true}},
{name: 'lat', value: {stringValue: location.Lat.toString()}},
{name: 'lng', value: {stringValue: location.Lng.toString()}},
{name: 'geohash', value: {stringValue: location.Country}},
{name: 'obj', value: {stringValue: location.Country}}
/* more columns */
]
})
// Create the SQL statement to run for all the items to insert as per AWS Docs linked above
sqlStatement = `INSERT INTO Locality (code, synonyms, country, state, city, zone, ward, colony, pincode, lat, lng, geohash, obj) VALUES (:code, :synonyms, :country, :state, :city, :zone, :ward, :colony, :pincode, :lat, :lng, :geohash, :obj);`
var params = {
resourceArn: 'arn:aws:rds:...xxx', /* required */
secretArn: 'arn:aws:secretsmanager:...xxx', /* required */
sql: sqlStatement, /* required */
database: 'mydb',
parameterSets: localityParameterSets
};
rds.batchExecuteStatement(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}
这将返回以下错误
BadRequestException: 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 '('loc/ncr-newdelhi#patparganj@acharyaniketan', 'Acharya Niketan, Mayur Vihar, Ne' at line 2
at Object.extractError (/Users/angad737/Dropbox/Breadwinner/App Dev Data/Database Folder/GeoDynamoDB/node_modules/aws-sdk/lib/protocol/json.js:52:27)
at Request.extractError (/Users/angad737/Dropbox/Breadwinner/App Dev Data/Database Folder/GeoDynamoDB/node_modules/aws-sdk/lib/protocol/rest_json.js:55:8)
at Request.callListeners (/Users/angad737/Dropbox/Breadwinner/App Dev Data/Database Folder/GeoDynamoDB/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at Request.emit (/Users/angad737/Dropbox/Breadwinner/App Dev Data/Database Folder/GeoDynamoDB/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (/Users/angad737/Dropbox/Breadwinner/App Dev Data/Database Folder/GeoDynamoDB/node_modules/aws-sdk/lib/request.js:688:14)
at Request.transition (/Users/angad737/Dropbox/Breadwinner/App Dev Data/Database Folder/GeoDynamoDB/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/Users/angad737/Dropbox/Breadwinner/App Dev Data/Database Folder/GeoDynamoDB/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /Users/angad737/Dropbox/Breadwinner/App Dev Data/Database Folder/GeoDynamoDB/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/Users/angad737/Dropbox/Breadwinner/App Dev Data/Database Folder/GeoDynamoDB/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/Users/angad737/Dropbox/Breadwinner/App Dev Data/Database Folder/GeoDynamoDB/node_modules/aws-sdk/lib/request.js:690:12) {
code: 'BadRequestException',
time: 2020-10-19T05:28:35.734Z,
requestId: '9e57d418-1018-49e0-a5bd-717ab1cf3ac4',
statusCode: 400,
retryable: false,
retryDelay: 94.67460516360629
我在 AWS Query 编辑器中运行了我希望 SQL 查询出现的内容,以检查任何语法错误,但此查询运行成功。
INSERT INTO Locality (code, synonyms, country, state, city, zone, ward, colony, pincode, lat, lng, geohash) VALUES
('loc/ncr-newdelhi#patparganj@acharyaniketan',
'Acharya Niketan, Mayur Vihar, New Delhi, Delhi 110091',
'India',
'New Delhi',
'New Delhi',
'Shahadra South',
'Patparganj',
'Acharya Niketan',
'undefined',
'28.6081393',
'77.29474669999999',
'India');
起初我以为它可能是“未定义”作为保留关键字,但事实并非如此。我觉得我遗漏了一些明显的东西,因为它应该是语法错误,但我无法发现它。做我的 StackOverflow 天使。
谢谢