Python 2.7 - insert multiple values in MySql in one execution from dictionary key values












0















I am trying to make dynamic insert command into mysql, from dictionary values in one transaction but getting an error. Also I was wondering what is the most efficient way to perform this specific case (maybe my code is not optimal)? I am using FOR since in some cases dictionary can be empty. Thanks



    import mysql.connector
mydb = mysql.connector.connect(..........
mycursor = mydb.cursor()
varStatic="test"
cust={'74.2': '54', '172.26': '76', '7': 'B9'}
insertStatement='"""INSERT INTO customers (id,number,desc) VALUES (%s,%s,%s)"""'
for key in cust:
insertStatement+=',('+key+','+cust[key]+','+varStatic+')'
mycursor.execute(insertStatement)
mydb.commit()









share|improve this question

























  • what error do you get?

    – Red Cricket
    Nov 19 '18 at 20:43











  • mysql.connector.errors.ProgrammingError: 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 '"""REPLACE INTO customers (id,number,desc) VALUES (%s,%s,%s)""",(74.2,54' at line 1

    – Dejan
    Nov 19 '18 at 20:46











  • actually, not quite sure what is wrong but in the same time I do not have feeling that my code is based on best practice..

    – Dejan
    Nov 19 '18 at 20:47











  • That does look like a syntax error. Maybe you could add a debug print statement something like print insertStatement and update your question with the output. Your using ''s so the """ ends up in your string.

    – Red Cricket
    Nov 19 '18 at 20:49













  • I think that maybe this is because values KEY, VALUE, VARSTATIC are not quoted at all

    – Dejan
    Nov 19 '18 at 20:54
















0















I am trying to make dynamic insert command into mysql, from dictionary values in one transaction but getting an error. Also I was wondering what is the most efficient way to perform this specific case (maybe my code is not optimal)? I am using FOR since in some cases dictionary can be empty. Thanks



    import mysql.connector
mydb = mysql.connector.connect(..........
mycursor = mydb.cursor()
varStatic="test"
cust={'74.2': '54', '172.26': '76', '7': 'B9'}
insertStatement='"""INSERT INTO customers (id,number,desc) VALUES (%s,%s,%s)"""'
for key in cust:
insertStatement+=',('+key+','+cust[key]+','+varStatic+')'
mycursor.execute(insertStatement)
mydb.commit()









share|improve this question

























  • what error do you get?

    – Red Cricket
    Nov 19 '18 at 20:43











  • mysql.connector.errors.ProgrammingError: 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 '"""REPLACE INTO customers (id,number,desc) VALUES (%s,%s,%s)""",(74.2,54' at line 1

    – Dejan
    Nov 19 '18 at 20:46











  • actually, not quite sure what is wrong but in the same time I do not have feeling that my code is based on best practice..

    – Dejan
    Nov 19 '18 at 20:47











  • That does look like a syntax error. Maybe you could add a debug print statement something like print insertStatement and update your question with the output. Your using ''s so the """ ends up in your string.

    – Red Cricket
    Nov 19 '18 at 20:49













  • I think that maybe this is because values KEY, VALUE, VARSTATIC are not quoted at all

    – Dejan
    Nov 19 '18 at 20:54














0












0








0








I am trying to make dynamic insert command into mysql, from dictionary values in one transaction but getting an error. Also I was wondering what is the most efficient way to perform this specific case (maybe my code is not optimal)? I am using FOR since in some cases dictionary can be empty. Thanks



    import mysql.connector
mydb = mysql.connector.connect(..........
mycursor = mydb.cursor()
varStatic="test"
cust={'74.2': '54', '172.26': '76', '7': 'B9'}
insertStatement='"""INSERT INTO customers (id,number,desc) VALUES (%s,%s,%s)"""'
for key in cust:
insertStatement+=',('+key+','+cust[key]+','+varStatic+')'
mycursor.execute(insertStatement)
mydb.commit()









share|improve this question
















I am trying to make dynamic insert command into mysql, from dictionary values in one transaction but getting an error. Also I was wondering what is the most efficient way to perform this specific case (maybe my code is not optimal)? I am using FOR since in some cases dictionary can be empty. Thanks



    import mysql.connector
mydb = mysql.connector.connect(..........
mycursor = mydb.cursor()
varStatic="test"
cust={'74.2': '54', '172.26': '76', '7': 'B9'}
insertStatement='"""INSERT INTO customers (id,number,desc) VALUES (%s,%s,%s)"""'
for key in cust:
insertStatement+=',('+key+','+cust[key]+','+varStatic+')'
mycursor.execute(insertStatement)
mydb.commit()






python mysql python-2.7 dictionary insert






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 21:31







Dejan

















asked Nov 19 '18 at 20:39









DejanDejan

58082149




58082149













  • what error do you get?

    – Red Cricket
    Nov 19 '18 at 20:43











  • mysql.connector.errors.ProgrammingError: 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 '"""REPLACE INTO customers (id,number,desc) VALUES (%s,%s,%s)""",(74.2,54' at line 1

    – Dejan
    Nov 19 '18 at 20:46











  • actually, not quite sure what is wrong but in the same time I do not have feeling that my code is based on best practice..

    – Dejan
    Nov 19 '18 at 20:47











  • That does look like a syntax error. Maybe you could add a debug print statement something like print insertStatement and update your question with the output. Your using ''s so the """ ends up in your string.

    – Red Cricket
    Nov 19 '18 at 20:49













  • I think that maybe this is because values KEY, VALUE, VARSTATIC are not quoted at all

    – Dejan
    Nov 19 '18 at 20:54



















  • what error do you get?

    – Red Cricket
    Nov 19 '18 at 20:43











  • mysql.connector.errors.ProgrammingError: 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 '"""REPLACE INTO customers (id,number,desc) VALUES (%s,%s,%s)""",(74.2,54' at line 1

    – Dejan
    Nov 19 '18 at 20:46











  • actually, not quite sure what is wrong but in the same time I do not have feeling that my code is based on best practice..

    – Dejan
    Nov 19 '18 at 20:47











  • That does look like a syntax error. Maybe you could add a debug print statement something like print insertStatement and update your question with the output. Your using ''s so the """ ends up in your string.

    – Red Cricket
    Nov 19 '18 at 20:49













  • I think that maybe this is because values KEY, VALUE, VARSTATIC are not quoted at all

    – Dejan
    Nov 19 '18 at 20:54

















what error do you get?

– Red Cricket
Nov 19 '18 at 20:43





what error do you get?

– Red Cricket
Nov 19 '18 at 20:43













mysql.connector.errors.ProgrammingError: 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 '"""REPLACE INTO customers (id,number,desc) VALUES (%s,%s,%s)""",(74.2,54' at line 1

– Dejan
Nov 19 '18 at 20:46





mysql.connector.errors.ProgrammingError: 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 '"""REPLACE INTO customers (id,number,desc) VALUES (%s,%s,%s)""",(74.2,54' at line 1

– Dejan
Nov 19 '18 at 20:46













actually, not quite sure what is wrong but in the same time I do not have feeling that my code is based on best practice..

– Dejan
Nov 19 '18 at 20:47





actually, not quite sure what is wrong but in the same time I do not have feeling that my code is based on best practice..

– Dejan
Nov 19 '18 at 20:47













That does look like a syntax error. Maybe you could add a debug print statement something like print insertStatement and update your question with the output. Your using ''s so the """ ends up in your string.

– Red Cricket
Nov 19 '18 at 20:49







That does look like a syntax error. Maybe you could add a debug print statement something like print insertStatement and update your question with the output. Your using ''s so the """ ends up in your string.

– Red Cricket
Nov 19 '18 at 20:49















I think that maybe this is because values KEY, VALUE, VARSTATIC are not quoted at all

– Dejan
Nov 19 '18 at 20:54





I think that maybe this is because values KEY, VALUE, VARSTATIC are not quoted at all

– Dejan
Nov 19 '18 at 20:54












1 Answer
1






active

oldest

votes


















1














You may do something like this, but a little confused about how to optimize for-loop and value. If i can get rid of append or replace to List Comprehensions, then you can use insertStatement += ("(%s,%s,%s),"*len(cust.items()))[:-1]



import mysql.connector
mydb = mysql.connector.connect(user="k",passwd="k",db="k")
mycursor = mydb.cursor()
varStatic="test"
cust={'74.2': "54'", '172.26': '76', '7': 'B9'}
insertStatement= """INSERT INTO customers (id,number,desc) VALUES """
value =
for k,v in cust.items():
insertStatement += "(%s,%s,%s),"
value.append(k)
value.append(v)
value.append(varStatic)

print(insertStatement[:-1],value)
try:
mycursor.execute(insertStatement[:-1],value)
mydb.commit()
except Exception as e:
print(e)
mydb.rollback()





share|improve this answer
























  • Hi its not working. This is for example output for two elements in dictionary. Something is not properly defined. This is what I am getting with Print statement. INSERT INTO customers (id,number,desc) VALUES (%s,%s,%s),(%s,%s,%s) ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test']

    – Dejan
    Nov 20 '18 at 8:31













  • print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) this is really not correct

    – Dejan
    Nov 20 '18 at 8:33













  • you should read this, and not recommend execute sql straight it will cause injection dev.mysql.com/doc/connector-python/en/… @Dejan

    – kcorlidy
    Nov 20 '18 at 8:40













  • I do not understand quite what you want to say? Are you suggesting to do insert one by one? your print of insert statement returns tis very strange and not correct format. Can you please correct it because answer is definetely not OK? print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) .

    – Dejan
    Nov 20 '18 at 8:58











  • mycursor.execute() will pass ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test '] into INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) . And you don't care about the data type, the function will automatically convert. Of course you can also execute your own sql, but you need to manually process the data to avoid SQL injection. btw, I will not post code I have never run, especially full code. @Dejande

    – kcorlidy
    Nov 20 '18 at 9:08













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%2f53382296%2fpython-2-7-insert-multiple-values-in-mysql-in-one-execution-from-dictionary-ke%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









1














You may do something like this, but a little confused about how to optimize for-loop and value. If i can get rid of append or replace to List Comprehensions, then you can use insertStatement += ("(%s,%s,%s),"*len(cust.items()))[:-1]



import mysql.connector
mydb = mysql.connector.connect(user="k",passwd="k",db="k")
mycursor = mydb.cursor()
varStatic="test"
cust={'74.2': "54'", '172.26': '76', '7': 'B9'}
insertStatement= """INSERT INTO customers (id,number,desc) VALUES """
value =
for k,v in cust.items():
insertStatement += "(%s,%s,%s),"
value.append(k)
value.append(v)
value.append(varStatic)

print(insertStatement[:-1],value)
try:
mycursor.execute(insertStatement[:-1],value)
mydb.commit()
except Exception as e:
print(e)
mydb.rollback()





share|improve this answer
























  • Hi its not working. This is for example output for two elements in dictionary. Something is not properly defined. This is what I am getting with Print statement. INSERT INTO customers (id,number,desc) VALUES (%s,%s,%s),(%s,%s,%s) ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test']

    – Dejan
    Nov 20 '18 at 8:31













  • print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) this is really not correct

    – Dejan
    Nov 20 '18 at 8:33













  • you should read this, and not recommend execute sql straight it will cause injection dev.mysql.com/doc/connector-python/en/… @Dejan

    – kcorlidy
    Nov 20 '18 at 8:40













  • I do not understand quite what you want to say? Are you suggesting to do insert one by one? your print of insert statement returns tis very strange and not correct format. Can you please correct it because answer is definetely not OK? print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) .

    – Dejan
    Nov 20 '18 at 8:58











  • mycursor.execute() will pass ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test '] into INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) . And you don't care about the data type, the function will automatically convert. Of course you can also execute your own sql, but you need to manually process the data to avoid SQL injection. btw, I will not post code I have never run, especially full code. @Dejande

    – kcorlidy
    Nov 20 '18 at 9:08


















1














You may do something like this, but a little confused about how to optimize for-loop and value. If i can get rid of append or replace to List Comprehensions, then you can use insertStatement += ("(%s,%s,%s),"*len(cust.items()))[:-1]



import mysql.connector
mydb = mysql.connector.connect(user="k",passwd="k",db="k")
mycursor = mydb.cursor()
varStatic="test"
cust={'74.2': "54'", '172.26': '76', '7': 'B9'}
insertStatement= """INSERT INTO customers (id,number,desc) VALUES """
value =
for k,v in cust.items():
insertStatement += "(%s,%s,%s),"
value.append(k)
value.append(v)
value.append(varStatic)

print(insertStatement[:-1],value)
try:
mycursor.execute(insertStatement[:-1],value)
mydb.commit()
except Exception as e:
print(e)
mydb.rollback()





share|improve this answer
























  • Hi its not working. This is for example output for two elements in dictionary. Something is not properly defined. This is what I am getting with Print statement. INSERT INTO customers (id,number,desc) VALUES (%s,%s,%s),(%s,%s,%s) ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test']

    – Dejan
    Nov 20 '18 at 8:31













  • print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) this is really not correct

    – Dejan
    Nov 20 '18 at 8:33













  • you should read this, and not recommend execute sql straight it will cause injection dev.mysql.com/doc/connector-python/en/… @Dejan

    – kcorlidy
    Nov 20 '18 at 8:40













  • I do not understand quite what you want to say? Are you suggesting to do insert one by one? your print of insert statement returns tis very strange and not correct format. Can you please correct it because answer is definetely not OK? print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) .

    – Dejan
    Nov 20 '18 at 8:58











  • mycursor.execute() will pass ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test '] into INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) . And you don't care about the data type, the function will automatically convert. Of course you can also execute your own sql, but you need to manually process the data to avoid SQL injection. btw, I will not post code I have never run, especially full code. @Dejande

    – kcorlidy
    Nov 20 '18 at 9:08
















1












1








1







You may do something like this, but a little confused about how to optimize for-loop and value. If i can get rid of append or replace to List Comprehensions, then you can use insertStatement += ("(%s,%s,%s),"*len(cust.items()))[:-1]



import mysql.connector
mydb = mysql.connector.connect(user="k",passwd="k",db="k")
mycursor = mydb.cursor()
varStatic="test"
cust={'74.2': "54'", '172.26': '76', '7': 'B9'}
insertStatement= """INSERT INTO customers (id,number,desc) VALUES """
value =
for k,v in cust.items():
insertStatement += "(%s,%s,%s),"
value.append(k)
value.append(v)
value.append(varStatic)

print(insertStatement[:-1],value)
try:
mycursor.execute(insertStatement[:-1],value)
mydb.commit()
except Exception as e:
print(e)
mydb.rollback()





share|improve this answer













You may do something like this, but a little confused about how to optimize for-loop and value. If i can get rid of append or replace to List Comprehensions, then you can use insertStatement += ("(%s,%s,%s),"*len(cust.items()))[:-1]



import mysql.connector
mydb = mysql.connector.connect(user="k",passwd="k",db="k")
mycursor = mydb.cursor()
varStatic="test"
cust={'74.2': "54'", '172.26': '76', '7': 'B9'}
insertStatement= """INSERT INTO customers (id,number,desc) VALUES """
value =
for k,v in cust.items():
insertStatement += "(%s,%s,%s),"
value.append(k)
value.append(v)
value.append(varStatic)

print(insertStatement[:-1],value)
try:
mycursor.execute(insertStatement[:-1],value)
mydb.commit()
except Exception as e:
print(e)
mydb.rollback()






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 8:21









kcorlidykcorlidy

2,2782418




2,2782418













  • Hi its not working. This is for example output for two elements in dictionary. Something is not properly defined. This is what I am getting with Print statement. INSERT INTO customers (id,number,desc) VALUES (%s,%s,%s),(%s,%s,%s) ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test']

    – Dejan
    Nov 20 '18 at 8:31













  • print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) this is really not correct

    – Dejan
    Nov 20 '18 at 8:33













  • you should read this, and not recommend execute sql straight it will cause injection dev.mysql.com/doc/connector-python/en/… @Dejan

    – kcorlidy
    Nov 20 '18 at 8:40













  • I do not understand quite what you want to say? Are you suggesting to do insert one by one? your print of insert statement returns tis very strange and not correct format. Can you please correct it because answer is definetely not OK? print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) .

    – Dejan
    Nov 20 '18 at 8:58











  • mycursor.execute() will pass ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test '] into INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) . And you don't care about the data type, the function will automatically convert. Of course you can also execute your own sql, but you need to manually process the data to avoid SQL injection. btw, I will not post code I have never run, especially full code. @Dejande

    – kcorlidy
    Nov 20 '18 at 9:08





















  • Hi its not working. This is for example output for two elements in dictionary. Something is not properly defined. This is what I am getting with Print statement. INSERT INTO customers (id,number,desc) VALUES (%s,%s,%s),(%s,%s,%s) ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test']

    – Dejan
    Nov 20 '18 at 8:31













  • print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) this is really not correct

    – Dejan
    Nov 20 '18 at 8:33













  • you should read this, and not recommend execute sql straight it will cause injection dev.mysql.com/doc/connector-python/en/… @Dejan

    – kcorlidy
    Nov 20 '18 at 8:40













  • I do not understand quite what you want to say? Are you suggesting to do insert one by one? your print of insert statement returns tis very strange and not correct format. Can you please correct it because answer is definetely not OK? print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) .

    – Dejan
    Nov 20 '18 at 8:58











  • mycursor.execute() will pass ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test '] into INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) . And you don't care about the data type, the function will automatically convert. Of course you can also execute your own sql, but you need to manually process the data to avoid SQL injection. btw, I will not post code I have never run, especially full code. @Dejande

    – kcorlidy
    Nov 20 '18 at 9:08



















Hi its not working. This is for example output for two elements in dictionary. Something is not properly defined. This is what I am getting with Print statement. INSERT INTO customers (id,number,desc) VALUES (%s,%s,%s),(%s,%s,%s) ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test']

– Dejan
Nov 20 '18 at 8:31







Hi its not working. This is for example output for two elements in dictionary. Something is not properly defined. This is what I am getting with Print statement. INSERT INTO customers (id,number,desc) VALUES (%s,%s,%s),(%s,%s,%s) ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test']

– Dejan
Nov 20 '18 at 8:31















print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) this is really not correct

– Dejan
Nov 20 '18 at 8:33







print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) this is really not correct

– Dejan
Nov 20 '18 at 8:33















you should read this, and not recommend execute sql straight it will cause injection dev.mysql.com/doc/connector-python/en/… @Dejan

– kcorlidy
Nov 20 '18 at 8:40







you should read this, and not recommend execute sql straight it will cause injection dev.mysql.com/doc/connector-python/en/… @Dejan

– kcorlidy
Nov 20 '18 at 8:40















I do not understand quite what you want to say? Are you suggesting to do insert one by one? your print of insert statement returns tis very strange and not correct format. Can you please correct it because answer is definetely not OK? print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) .

– Dejan
Nov 20 '18 at 8:58





I do not understand quite what you want to say? Are you suggesting to do insert one by one? your print of insert statement returns tis very strange and not correct format. Can you please correct it because answer is definetely not OK? print(insertStatement[:-1]) returns INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) .

– Dejan
Nov 20 '18 at 8:58













mycursor.execute() will pass ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test '] into INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) . And you don't care about the data type, the function will automatically convert. Of course you can also execute your own sql, but you need to manually process the data to avoid SQL injection. btw, I will not post code I have never run, especially full code. @Dejande

– kcorlidy
Nov 20 '18 at 9:08







mycursor.execute() will pass ['172.26.74.2111', '54.10.EC.0C.1D.6B', 'test', '172.26.74.112232', '54.10.EC.54.92.FD', 'test '] into INSERT INTO customers (id,numbers,desc) VALUES (%s,%s,%s),(%s,%s,%s) . And you don't care about the data type, the function will automatically convert. Of course you can also execute your own sql, but you need to manually process the data to avoid SQL injection. btw, I will not post code I have never run, especially full code. @Dejande

– kcorlidy
Nov 20 '18 at 9:08






















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%2f53382296%2fpython-2-7-insert-multiple-values-in-mysql-in-one-execution-from-dictionary-ke%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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings