简体   繁体   English

如何使用 node.js 脚本将 JSON 字符串插入到 mysql?

[英]How to insert JSON string to mysql with node.js script?

I have a string like this:我有一个这样的字符串:

{
  WABrowserId: '"hTCl7asDPe/EIymrQC57Hg=="',
  WASecretBundle: '{"key":"o65mhfj8XZpuCIIgamAdsaibobs0HKiiBCwTAn36qms=","encKey":"nw5vM2sa05Eh94boiPO5r2qi5fS7CZmJwNNWgIsEj08=","macKey":"o65mhfj8XZpuCIIgamAd3Eibobs0HKiiBCwTAn36qms="}',
  WAToken1: '"ArRasrW9r63ByrbKDAauchBnzEUYOO9q0HTWJYfG0RM="',
  WAToken2: '"1@GGZtYQss1DkFVbXvuH28Dmm6YdI6wkHvqN1lSbAVAj+S4N5g3qQwuEAdQBsEp/j1cPVRu4bMexECrQ=="'
}

I got an error message like this:我收到这样的错误消息:

UnhandledPromiseRejectionWarning: Error: You have an error in your SQL syntax; UnhandledPromiseRejectionWarning: 错误:您的 SQL 语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near ' WABrowserId = '"hTCl7AYDPe/EIymrQC57Hg=="', WASecretBundle = '{"key":"o' at line 1查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的 ' WABrowserId = '"hTCl7AYDPe/EIymrQC57Hg=="', WASecretBundle = '{"key":"o' 附近使用的正确语法

How can I insert this string into a MySQL database with a Node.js script?如何使用 Node.js 脚本将此字符串插入到 MySQL 数据库中?

You can't insert normal JS object, you need to parse into JSON using JSON.stringify(obj) function then insert into the database.你不能插入普通的 JS object,你需要使用JSON.stringify(obj) function 解析成 JSON 然后插入到数据库中。

you might also need to remove the single quotes and use double quotes.您可能还需要删除单引号并使用双引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM