python - Python 中的 JSON 到 MySQL
问题描述
我需要将所有响应从 Websocket Cilent 插入到 MySQL 数据库。不幸的是,我在互联网上没有找到任何可以帮助我的东西。到目前为止我的代码:
def to_db(self):
cur = self.db.cursor()
db = self.db
while True:
data = self.ws.recv()
print(data)
sql = f"INSERT INTO `db`.`test` (`output`) VALUES ({data});"
cur.execute(sql)
db.commit()
db.close()
当我运行脚本时,我收到以下错误消息:
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '"event":"info","version":2,"serverId":"e7caa4g8-7642-45f5-abc9-c9f3ad325274","pl' at line 1
我这样创建了我的数据库:
create table websocket_test
(
output json null
);
解决方案
推荐阅读
- ruby - 如何将所有文件和子文件夹从一个文件夹复制到 Ruby 中同一 AWS S3 存储桶中的另一个文件夹
- android - Android ViewModel 如何确保它必须绑定相同的 Activity 或 Fragment(在屏幕旋转的情况下)?
- javascript - Prebid.js - 一个接一个地运行多个拍卖
- windows - 在 Windows Cygwin 中使用 XARGS 执行多个命令
- java - Java替换/改进空检查
- c++ - 对成员变量使用 std::enable_if 或类似方法
- javascript - 没有空的对象的函数属性
- r - 'utils' 的 loadNamespace() 中的 onLoad 失败,详细信息
- azure - Azure 函数为本地驱动器上传文件
- typescript - RxJS - 每 3 秒查询服务器 15 秒或肯定结果