首页 > 解决方案 > 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
);

标签: pythonmysqljsonwebsocket

解决方案


推荐阅读