简体   繁体   English

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

[英]Issue with Inserting JSON data into MySQL database using Python, error in your SQL syntax

I am trying to insert some JSON data into a MySQL database using a python script.我正在尝试使用 python 脚本将一些 JSON 数据插入到 MySQL 数据库中。 Seems like it is close to working, but I keep getting an error that says:似乎快要工作了,但我不断收到一个错误消息:

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

That is output by the try / catch in the script:这是脚本中 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)    

I put some debugging stuff in the script:我在脚本中放了一些调试的东西:

    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)

and it prints out <class 'str'> as the datatype for all of the values that I want to insert, and they all validate as JSON using jsonlint.它打印出 <class 'str'> 作为我想要插入的所有值的数据类型,并且它们都使用 jsonlint 验证为 JSON。

I have a lot more details, and I've played around with changing the tables in the database from JSON to VARCHAR, etc and that does not help.我有更多的细节,我已经尝试将数据库中的表从 JSON 更改为 VARCHAR 等,但这无济于事。 Also played around with using json.dump(var), etc., so it seems like it must just be s syntax issue with:还使用了 json.dump(var) 等,所以看起来它必须只是语法问题:

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

statement, which is what the 1064 means really.声明,这就是 1064 的真正含义。 Probably something simple, I hope, otherwise I can provide more details.我希望可能是简单的事情,否则我可以提供更多细节。

As an example, a snippet of dataset_out is as follows.例如,dataset_out 的一个片段如下。 That is what it shows in the terminal via Docker when I print(dataset_out), but turncated it a bit.这就是它在我打印(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" is not a valid table name. “n-create”不是有效的表名。 I suspect you meant "n_create".我怀疑你的意思是“n_create”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM