【发布时间】:2020-03-31 22:14:25
【问题描述】:
我正在尝试将 JSON 作为字符串插入 Azure SQL 数据库的表中。 “信息”列被创建为 nvarchar(max)。但是我尝试了两种方式都出现错误:
尝试 1: 将 JSON 作为对象发送
INSERT INTO productsTable (Id, Name, Url, Info) VALUES (123456, 'Name of product', 'https://www.example.com/product/123456', {'test':'here','nested':{'itest':' ivalue','itest2':100}})
导致错误:
"error": {
"code": "EREQUEST",
"originalError": {
"info": {
"name": "ERROR",
"event": "errorMessage",
"number": 102,
"state": 1,
"class": 15,
"message": "Incorrect syntax near 'test'.",
"serverName": "myapp",
"procName": "",
"lineNumber": 1
}
}
尝试 2: JSON.stringify json 在发送写入之前。
INSERT INTO CampaignDetailsTable (Id, Name, Url, Info) VALUES (123456, 'Name of produc', 'https://www.example.com/product/123456', '{\'test\':\'here\',\'nested\':{\'itest\':\'ivalue\',\'itest2\':100}}')
导致同样的错误:
"code": "EREQUEST",
"originalError": {
"info": {
"name": "ERROR",
"event": "errorMessage",
"number": 102,
"state": 1,
"class": 15,
"message": "Incorrect syntax near 'test'.",
"serverName": "myapp",
"procName": "",
"lineNumber": 1
}
}
我不关心它以什么格式存储在 SQL 中,只要我读回代码后可以在我的代码中恢复为 JSON 对象即可。
【问题讨论】:
-
它只是一个字符串,所以当您进行文字插入时,它遵循所有转义引号的规则。 stackoverflow.com/questions/1586560/…。此外,由于它是一个字符串,因此必须用引号括起来。
-
您可以在此处发布:freeformatter.com/sql-escape.html。复制结果,并在其周围加上另一组引号。
标签: node.js json sql-server