在表插入

问题描述

我需要将数据从json文件加载到名为Locality的MysqL数据库表中。下面的代码返回一个MysqL语法错误。适用于aurora MysqL Serverless的MysqL版本5.7.30。我正在使用aws-sdk,导入所有库,创建RDS实例。但是由于某种原因,它会返回sql语法错误

如果我能看到rds.batchExecuteStatement作为sql产生的内容,这也将有所帮助。我已经尝试过了,但是找不到sql语句是什么。 (帮助?)

我已从以下AWS文档中将python转换为javascript Using the Data API for RDS Serverless

快速链接查看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',{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

我运行了我希望SQL查询在AWS Query编辑器中执行的操作,以检查是否存在语法错误,但是此查询成功运行。

INSERT INTO Locality (code,geohash) VALUES 
('loc/ncr-newdelhi#patparganj@acharyaniketan',New Delhi,Delhi 110091','India','New Delhi','Shahadra South','Patparganj','Acharya Niketan','undefined','28.6081393','77.29474669999999','India');

起初我以为可能是保留关键字“未定义”,但事实并非如此。我觉得我缺少一些明显的东西,因为它应该是语法错误,但我无法发现它。成为我的StackOverflow天使。

谢谢

解决方法

您忘记向sqlStatement 函数提供SQL查询(rds.batchExecuteStatement),您为其提供了参数,但是没有SQL代码可将其应用于。

,

我遇到了同样的问题。调试了好几个小时才发现你的sql语句末尾不能包含分号...

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...