使用Python将JSON数据插入MySQL数据库时出现问题,SQL语法错误



我正试图使用python脚本将一些JSON数据插入MySQL数据库。看起来它已经接近工作了,但我一直收到一个错误,上面写着:

python_mpps_1         | Error Code: 1064
python_mpps_1         | SQLSTATE 42000
python_mpps_1         | Message 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 '-create (dataset_in, mwl, dataset_out) VALUES ('{"00000000": {"Value": [2], ' at line 1

这是由脚本中的try/catch输出的:

try:
mycursor.execute("INSERT INTO n-create (dataset_in, mwl, dataset_out) VALUES (%s, %s, %s)", (dataset_in, mwl , dataset_out))
mydb.commit()
print("Inserting N_CREATE" + str(mycursor.rowcount))
mycursor.close()
except mysql.connector.Error as err:
print(err)
print("Error Code:", err.errno)
print("SQLSTATE", err.sqlstate)
print("Message", err.msg)    

我在脚本中放入了一些调试内容:

mydb = mysql.connector.connect(host="mysql_db", port = 3306, user="",password="",database="")
mycursor = mydb.cursor()
print(type(dataset_in))
print(dataset_in)
print(type(dataset_out))
print(dataset_out)
mwl = json.dumps(mwl[0])
print(type(mwl))
print(mwl)

并且它打印出<类"str">作为我要插入的所有值的数据类型,并且它们都使用jsonint验证为JSON。

我有更多的细节,我已经尝试过将数据库中的表从JSON更改为VARCHAR等,但这并没有帮助。还使用json.dump(var(等,所以看起来这一定只是的语法问题

mycursor.execute("INSERT INTO n-create (dataset_in, mwl, dataset_out) VALUES (%s, %s, %s)", (dataset_in, mwl , dataset_out))

这就是1064的真正含义。我希望可能是一些简单的事情,否则我可以提供更多的细节。

例如,dataset_out的一个片段如下。这就是当我打印(dataset_out(时,它通过Docker在终端中显示的内容,但稍微改变了一下。

{
"00000000": {
"Value": [2],
"vr": "UL"
},
"00000002": {
"Value": ["1.2.840.10008.3.1.2.3.3"],
"vr": "UI"
},
"00000100": {
"Value": [33088],
"vr": "US"
},
"00000120": {
"Value": [8],
"vr": "US"
},
"00000800": {
"Value": [0],
"vr": "US"
},
"00000900": {
"Value": [0],
"vr": "US"
}
}

"n-create";不是有效的表名。我怀疑你的意思是";n_create";。

最新更新