Python对象到JSON到MySQL

Python对象到JSON到MySQL,python,mysql,json,Python,Mysql,Json,我在尝试将json插入MySQL数据库时遇到了一个问题,json是从带有json.dumps的python对象转换而来的。与数据库的连接正在使用另一个python文件。我已经尝试过只插入值,这是有效的,但是对于json文件,它不起作用。 我的Python文件: import json import dbConnection cur = dbConnection.cursor cnx = dbConnection.conn DEVICES = { "id": "1",

我在尝试将json插入MySQL数据库时遇到了一个问题,json是从带有json.dumps的python对象转换而来的。与数据库的连接正在使用另一个python文件。我已经尝试过只插入值,这是有效的,但是对于json文件,它不起作用。 我的Python文件:

import json
import dbConnection

cur = dbConnection.cursor
cnx = dbConnection.conn

DEVICES = {
        "id": "1",
        "isPoweredOn": "True",
    "os": "Linux"
}

j = json.dumps(DEVICES)
print(j)

sql = "INSERT INTO DEVICES (id, isPoweredOn, os) VALUES (%s, %s, %s)"


val = (json.dumps(DEVICES))


cur.execute(sql, val)
cnx.commit()
print(cur.rowcount, "record inserted.")
尝试执行时,我收到错误代码:

"id": "1", "isPoweredOn": "True", "os": "Linux"}
Traceback (most recent call last):
  File "dbInit.py", line 22, in <module>
    cur.execute(sql, val)
  File "/home/silvan/.virtualenvs/pyproj1/lib/python3.8/site-packages/mysql/connector/cursor.py", line 551, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "/home/silvan/.virtualenvs/pyproj1/lib/python3.8/site-packages/mysql/connector/connection.py", line 490, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "/home/silvan/.virtualenvs/pyproj1/lib/python3.8/site-packages/mysql/connector/connection.py", line 395, in _handle_result
    raise errors.get_exception(packet)
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 '%s, %s, %s)' at line 1
提前谢谢你的帮助

首先,将设备强制转换为dict,然后是格式

sql=插入到设备`id`、`isPoweredOn`、`os`值%ids、%isPoweredOns、%oss

然后执行它:

try:
    cur.execute(sql, DEVICES)
    cnx.commit()
except error:
    print(error)
干杯

您需要将json.loadsj赋值给一个变量,然后才能正确地访问这些值

尝试:

import json
import dbConnection

cur = dbConnection.cursor
cnx = dbConnection.conn


DEVICES = {
        "id": "1",
        "isPoweredOn": False ,
        "os": "Linux"
}
j = json.dumps(DEVICES)
values = json.loads(j) 
'''
# Quick debugging 
print(j , type(j))
print(values , type(values))
print(values['isPoweredOn'])
'''

sql = "INSERT INTO DEVICES (id, isPoweredOn, os) VALUES (%s, %s, %s)"

val = ( '' , values['isPoweredOn'] , values['os'])

cur.execute(sql, val)
cnx.commit()
print(cur.rowcount, "record inserted.")

此外,由于您将id定义为INT AUTO_INCREMENT主键而不是NULL,因此无法将设备id(其值为['id'])插入id列,如果确实需要存储值['id'],则可以更改设备表并创建一个名为device_id的新列来存储设备id

如果操作系统是一个varchar,我认为应该用单引号%s、%s、%s对其进行排序。还要注意,True可能不起作用。@dcg谢谢,尝试过了,但仍然是相同的错误。True实际上是一个布尔值,我已经用%r和python对象中没有任何双引号的an对它进行了测试。如果没有json,它的工作方式是这样的。不要插入val,而是这样做:cur.executesql,DEVICES。@mvp刚刚尝试了这个方法,不幸的是仍然是相同的错误,我认为我必须将它从python转换为json。我尝试了这个方法,但它没有正常工作,但是上面的解决方案对我有效。无论如何,谢谢你的意见。
import json
import dbConnection

cur = dbConnection.cursor
cnx = dbConnection.conn


DEVICES = {
        "id": "1",
        "isPoweredOn": False ,
        "os": "Linux"
}
j = json.dumps(DEVICES)
values = json.loads(j) 
'''
# Quick debugging 
print(j , type(j))
print(values , type(values))
print(values['isPoweredOn'])
'''

sql = "INSERT INTO DEVICES (id, isPoweredOn, os) VALUES (%s, %s, %s)"

val = ( '' , values['isPoweredOn'] , values['os'])

cur.execute(sql, val)
cnx.commit()
print(cur.rowcount, "record inserted.")