[英]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.