【发布时间】:2017-08-29 08:35:40
【问题描述】:
在插入 Python 中使用的 SQL 数据库的表时出现以下错误:
pymysql.err.ProgrammingError: (1064, 'You have an error in your SQL
syntax; check the manual that corresponds to your MariaDB server
version for the right syntax to use near \'"Black": {"b": "125.98",
"a": "126.796", "L": "117.245"}, "Pink": {"b": "130.286\' at line 1')
SQL 命令是:
json1 = json.dumps(meanLAB_vals_dict) # convert python dict to json string
json2 = json.dumps(deltaE_dict)
sql_command = """INSERT INTO data_integrity_tool VALUES (%d, %d, %s, %s)""" %(i, image_id, json1, json2)
cursor.execute(sql_command)
connection.commit()
而 meanLAB_vals_dict 是:
{'Black': {'b': '125.98', 'a': '126.796', 'L': '117.245'}, 'Pink':
{'b': '130.286', 'a': '180.918', 'L': '169.0'}, 'Green': {'b':
'135.531', 'a': '103.51', 'L': '144.755'}, 'Violet': {'b': '109.878',
'a': '136.653', 'L': '122.02'}, 'Grey': {'b': '123.327', 'a':
'125.612', 'L': '139.429'}, 'Yellow': {'b': '195.571', 'a':
'112.612', 'L': '234.694'}, 'Red': {'b': '153.449', 'a': '177.918',
'L': '163.939'}, 'White': {'b': '128.02', 'a': '128.939', 'L':
'243.878'}, 'Blue': {'b': '84.7551', 'a': '122.98', 'L': '163.673'}}
而 deltaE_dict 是:
{'Black': '38.5187', 'Pink': '38.6975', 'mean delta E': '28.0643',
'Green': '42.6365', 'Violet': '35.5018', 'Grey': '19.8903', 'Yellow':
'24.5115', 'Red': '40.0078', 'White': '4.4993', 'Blue': '8.31544'}
而 i 和 image_id 是两个整数(迭代的索引)。 以下是 data_integrity_tool 表:
sql_command = """CREATE TABLE IF NOT EXISTS data_integrity_tool (
id_ INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
image_id INTEGER NOT NULL,
mean_lab_values TEXT,
delta_e_values TEXT);"""
我知道已经存在一些类似的问题,但是,它们是针对 PHP 的,我对此一无所知,而且我是 SQL 的新手。
【问题讨论】:
-
字段名称/列是可选的。但是,我可以尝试一下。而且,结果是同样的错误。此外,与blob。错误仍然存在。
-
您在整数列
id_和image_id中传递字符串值。id_也是自动增量。 -
@SatishGarg:我做了这些改变。但是,错误仍然存在。似乎 json 对象中有一些东西。但是,当我将 python 字典转储到 json 对象中时,我没有收到任何错误。
-
你能发布你的查询部分吗?
-
是的,查询已经在问题的开头部分:sql_command = """INSERT INTO data_integrity_tool(id_, image_id, mean_lab_values, delta_e_values) VALUES (%d, %d, %s, % s)""" %(i, image_id, json1, json2)