简体   繁体   English

将INSERT TO sql与字典值一起使用

[英]Using INSERT TO sql with with dictionary values

I'm using Python 3.6 mysql 8 and mysql python connector 8 我正在使用Python 3.6 mysql 8和mysql python connector 8
I'm trying to insert to the following table 我正在尝试插入下表

| Recipes_id| title| Prep_time| servings | Summarize|  Very_healthy| Cuisine| img| source_url|
+-----------+------+----------+---------+----------+--------------+--------+----+-----------+

using a dictionary with different naming for the keys. 对键使用不同命名的字典。

Ie a sample object of the dictionary: 即字典的样本对象:

{
    'cuisine': None, 
    'id': 521693, 
    'img': None, 
    'prep_time': 5,
    'servings': 1, 
    'source_url': None,
    'summarize': 'Mango, Banana, Rasp...thie</a>.', 
    'title': 'Mango, Banana, Rasp... Smoothie',
    'very_healthy': None
}

Here, the id key differs from the recipe_id column name in the table. 在此, id键与表中的recipe_id列名称不同。

MySQL query is as follows: MySQL查询如下:

Recipes_INSERT_SQL = (
    "INSERT INTO recipes "
    "(Recipes_id, title, Prep_time, servings, Summarize, Very_healthy, Cuisine, Img, Source_url) "
    "VALUES ( %(id)s, %(title)s, %(prep_time)s, %(servings)s, %(summarize)s, %(very_healthy)s, %(cuisine)s, %(img)s, %(source_url)s )"
)

cursor.execute(Recipes_INSERT_SQL, recipes_data)

Notice that the dictionary keys differ from the column names 请注意,字典键与列名不同
But the error is: 但是错误是:

ProgrammingError(1064, "1064 (42000): 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 '%(id)s,
%(title)s, %(prep_time)s, %(serving)s, %(summarize)s, %(very_healthy)s, ' at line 1", 
'42000') 

As you can see, the engine didn't even parsed the (title)s and other names. 如您所见,引擎甚至没有解析(title)和其他名称。
Am I missing something? 我想念什么吗? the placeholders convention for mysql 8 is %(name)s as stated in the documentary. 如纪录片中所述,mysql 8的占位符约定为%(name)s。

Here is the sql hierarchy and recipes table: 这是sql层次结构和配方表:
在此处输入图片说明

I think, based on your explanation, you have a group of recipes in a dictionary format: 我认为,根据您的解释,您可以使用字典格式的一组食谱:

The syntax you're using to insert values in database need to have directly the dictionary on the variable. 您用于在数据库中插入值的语法需要在变量上直接具有字典。 An example of invalid syntax are: 无效语法的一个示例是:

{
    'cuisine': None, 
    'id': 465645, 
},{
    'cuisine': None, 
    'id': 521693, 
}

If that's the case, you need to iterate the array of dictionaries, and with each value of it (each recipe), launch the SQL query. 在这种情况下,您需要迭代字典数组,并使用它的每个值(每个配方)启动SQL查询。 You cannot do only one query for insert all values. 您不能只执行一个查询来插入所有值。

You can use, for example, a foreach for iterate all values on your recipes_data, storing the current value on recipe (for example). 例如,您可以使用一个foreach来迭代recipes_data上的所有值,例如将当前值存储在配方上。 And then, inside the foreach, you can run the cursor.execute() 然后,在foreach中,您可以运行cursor.execute()

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

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