Using INSERT TO sql with with dictionary values












1















I'm using Python 3.6 mysql 8 and 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.



I.e 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.



MySQL query is as follows:



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.
Am I missing something? the placeholders convention for mysql 8 is %(name)s as stated in the documentary.


Here is the sql hierarchy and recipes table:
enter image description here










share|improve this question

























  • Similar kinda problem addressed stackoverflow.com/questions/9336270/…

    – Naresh Kumar
    Jan 1 at 13:36













  • No, because here the dict keys are not as the table keys. I'm trying to use this syntax->dev.mysql.com/doc/connector-python/en/…

    – DsCpp
    Jan 1 at 13:45
















1















I'm using Python 3.6 mysql 8 and 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.



I.e 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.



MySQL query is as follows:



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.
Am I missing something? the placeholders convention for mysql 8 is %(name)s as stated in the documentary.


Here is the sql hierarchy and recipes table:
enter image description here










share|improve this question

























  • Similar kinda problem addressed stackoverflow.com/questions/9336270/…

    – Naresh Kumar
    Jan 1 at 13:36













  • No, because here the dict keys are not as the table keys. I'm trying to use this syntax->dev.mysql.com/doc/connector-python/en/…

    – DsCpp
    Jan 1 at 13:45














1












1








1








I'm using Python 3.6 mysql 8 and 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.



I.e 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.



MySQL query is as follows:



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.
Am I missing something? the placeholders convention for mysql 8 is %(name)s as stated in the documentary.


Here is the sql hierarchy and recipes table:
enter image description here










share|improve this question
















I'm using Python 3.6 mysql 8 and 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.



I.e 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.



MySQL query is as follows:



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.
Am I missing something? the placeholders convention for mysql 8 is %(name)s as stated in the documentary.


Here is the sql hierarchy and recipes table:
enter image description here







python mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 21:11







DsCpp

















asked Jan 1 at 13:26









DsCppDsCpp

453211




453211













  • Similar kinda problem addressed stackoverflow.com/questions/9336270/…

    – Naresh Kumar
    Jan 1 at 13:36













  • No, because here the dict keys are not as the table keys. I'm trying to use this syntax->dev.mysql.com/doc/connector-python/en/…

    – DsCpp
    Jan 1 at 13:45



















  • Similar kinda problem addressed stackoverflow.com/questions/9336270/…

    – Naresh Kumar
    Jan 1 at 13:36













  • No, because here the dict keys are not as the table keys. I'm trying to use this syntax->dev.mysql.com/doc/connector-python/en/…

    – DsCpp
    Jan 1 at 13:45

















Similar kinda problem addressed stackoverflow.com/questions/9336270/…

– Naresh Kumar
Jan 1 at 13:36







Similar kinda problem addressed stackoverflow.com/questions/9336270/…

– Naresh Kumar
Jan 1 at 13:36















No, because here the dict keys are not as the table keys. I'm trying to use this syntax->dev.mysql.com/doc/connector-python/en/…

– DsCpp
Jan 1 at 13:45





No, because here the dict keys are not as the table keys. I'm trying to use this syntax->dev.mysql.com/doc/connector-python/en/…

– DsCpp
Jan 1 at 13:45












1 Answer
1






active

oldest

votes


















0














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. 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). And then, inside the foreach, you can run the cursor.execute()






share|improve this answer
























  • That's what I'm doing../ foreach dict I launch the query

    – DsCpp
    Jan 1 at 18:38











  • Try to debug it a bit, printing on screen the value of your recipes_data inside the foreach

    – Sakura Kinomoto
    Jan 1 at 18:39











  • The sampled dict is also the first one, I suspect that the cursor doesn't even parse the %(name)s, and ignore it as unknown syntax

    – DsCpp
    Jan 1 at 21:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995812%2fusing-insert-to-sql-with-with-dictionary-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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. 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). And then, inside the foreach, you can run the cursor.execute()






share|improve this answer
























  • That's what I'm doing../ foreach dict I launch the query

    – DsCpp
    Jan 1 at 18:38











  • Try to debug it a bit, printing on screen the value of your recipes_data inside the foreach

    – Sakura Kinomoto
    Jan 1 at 18:39











  • The sampled dict is also the first one, I suspect that the cursor doesn't even parse the %(name)s, and ignore it as unknown syntax

    – DsCpp
    Jan 1 at 21:12
















0














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. 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). And then, inside the foreach, you can run the cursor.execute()






share|improve this answer
























  • That's what I'm doing../ foreach dict I launch the query

    – DsCpp
    Jan 1 at 18:38











  • Try to debug it a bit, printing on screen the value of your recipes_data inside the foreach

    – Sakura Kinomoto
    Jan 1 at 18:39











  • The sampled dict is also the first one, I suspect that the cursor doesn't even parse the %(name)s, and ignore it as unknown syntax

    – DsCpp
    Jan 1 at 21:12














0












0








0







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. 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). And then, inside the foreach, you can run the cursor.execute()






share|improve this answer













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. 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). And then, inside the foreach, you can run the cursor.execute()







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 18:20









Sakura KinomotoSakura Kinomoto

1,0411122




1,0411122













  • That's what I'm doing../ foreach dict I launch the query

    – DsCpp
    Jan 1 at 18:38











  • Try to debug it a bit, printing on screen the value of your recipes_data inside the foreach

    – Sakura Kinomoto
    Jan 1 at 18:39











  • The sampled dict is also the first one, I suspect that the cursor doesn't even parse the %(name)s, and ignore it as unknown syntax

    – DsCpp
    Jan 1 at 21:12



















  • That's what I'm doing../ foreach dict I launch the query

    – DsCpp
    Jan 1 at 18:38











  • Try to debug it a bit, printing on screen the value of your recipes_data inside the foreach

    – Sakura Kinomoto
    Jan 1 at 18:39











  • The sampled dict is also the first one, I suspect that the cursor doesn't even parse the %(name)s, and ignore it as unknown syntax

    – DsCpp
    Jan 1 at 21:12

















That's what I'm doing../ foreach dict I launch the query

– DsCpp
Jan 1 at 18:38





That's what I'm doing../ foreach dict I launch the query

– DsCpp
Jan 1 at 18:38













Try to debug it a bit, printing on screen the value of your recipes_data inside the foreach

– Sakura Kinomoto
Jan 1 at 18:39





Try to debug it a bit, printing on screen the value of your recipes_data inside the foreach

– Sakura Kinomoto
Jan 1 at 18:39













The sampled dict is also the first one, I suspect that the cursor doesn't even parse the %(name)s, and ignore it as unknown syntax

– DsCpp
Jan 1 at 21:12





The sampled dict is also the first one, I suspect that the cursor doesn't even parse the %(name)s, and ignore it as unknown syntax

– DsCpp
Jan 1 at 21:12




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995812%2fusing-insert-to-sql-with-with-dictionary-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

ts Property 'filter' does not exist on type '{}'

mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window