Python 2.7 - insert multiple values in MySql in one execution from dictionary key values
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
|
show 3 more comments
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
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 likeprint insertStatementand 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
|
show 3 more comments
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
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
python mysql python-2.7 dictionary insert
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 likeprint insertStatementand 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
|
show 3 more comments
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 likeprint insertStatementand 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
|
show 3 more comments
1 Answer
1
active
oldest
votes
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()
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 ']intoINSERT 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
|
show 9 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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()
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 ']intoINSERT 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
|
show 9 more comments
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()
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 ']intoINSERT 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
|
show 9 more comments
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()
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()
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 ']intoINSERT 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
|
show 9 more comments
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 ']intoINSERT 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
|
show 9 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 insertStatementand 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