[英]Inserting CSV data into MySQL database - "You have an error in your SQL syntax; near ' ' at line 1
[英]Issue with Inserting JSON data into MySQL database using Python, error in your SQL syntax
我正在尝试使用 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)
它打印出 <class 'str'> 作为我想要插入的所有值的数据类型,并且它们都使用 jsonlint 验证为 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”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.