string formatting for SQL query [duplicate]
This question already has an answer here:
Python MySQLdb TypeError: not all arguments converted during string formatting
8 answers
I shoot a query via python like this:
cur.execute("INSERT INTO tblMQTTtest (string) VALUES ('test')")
con.commit()
which works fine. But if I come to the point and I will use a spaceholder, I get an error message :/
var1 = 'mystring'
cur.execute("INSERT INTO tblMQTTtest (string) VALUES (%s)", (var1))
con.commit()
Error:
File "mqtt1.py", line 25, in write
cur.execute("INSERT INTO tblMQTTtest (string) VALUES (%s)", (var1))
TypeError: not all arguments converted during string formatting
Any Ideas?
python sql python-3.x
marked as duplicate by khelwood, Community♦ Nov 23 '18 at 18:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Python MySQLdb TypeError: not all arguments converted during string formatting
8 answers
I shoot a query via python like this:
cur.execute("INSERT INTO tblMQTTtest (string) VALUES ('test')")
con.commit()
which works fine. But if I come to the point and I will use a spaceholder, I get an error message :/
var1 = 'mystring'
cur.execute("INSERT INTO tblMQTTtest (string) VALUES (%s)", (var1))
con.commit()
Error:
File "mqtt1.py", line 25, in write
cur.execute("INSERT INTO tblMQTTtest (string) VALUES (%s)", (var1))
TypeError: not all arguments converted during string formatting
Any Ideas?
python sql python-3.x
marked as duplicate by khelwood, Community♦ Nov 23 '18 at 18:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
(var1)
isn't a tuple. It's just a variable in parentheses. Should be(var1,)
– khelwood
Nov 23 '18 at 17:53
I didn't expected that I have to use a tuple for a single value. It works and it was driving me crazy. Thank you!
– Matthias
Nov 23 '18 at 17:56
While reading again the duplicate I can confirm it would have answered my question if I had read it with the background of knowing the tuple.
– Matthias
Nov 23 '18 at 18:04
add a comment |
This question already has an answer here:
Python MySQLdb TypeError: not all arguments converted during string formatting
8 answers
I shoot a query via python like this:
cur.execute("INSERT INTO tblMQTTtest (string) VALUES ('test')")
con.commit()
which works fine. But if I come to the point and I will use a spaceholder, I get an error message :/
var1 = 'mystring'
cur.execute("INSERT INTO tblMQTTtest (string) VALUES (%s)", (var1))
con.commit()
Error:
File "mqtt1.py", line 25, in write
cur.execute("INSERT INTO tblMQTTtest (string) VALUES (%s)", (var1))
TypeError: not all arguments converted during string formatting
Any Ideas?
python sql python-3.x
This question already has an answer here:
Python MySQLdb TypeError: not all arguments converted during string formatting
8 answers
I shoot a query via python like this:
cur.execute("INSERT INTO tblMQTTtest (string) VALUES ('test')")
con.commit()
which works fine. But if I come to the point and I will use a spaceholder, I get an error message :/
var1 = 'mystring'
cur.execute("INSERT INTO tblMQTTtest (string) VALUES (%s)", (var1))
con.commit()
Error:
File "mqtt1.py", line 25, in write
cur.execute("INSERT INTO tblMQTTtest (string) VALUES (%s)", (var1))
TypeError: not all arguments converted during string formatting
Any Ideas?
This question already has an answer here:
Python MySQLdb TypeError: not all arguments converted during string formatting
8 answers
python sql python-3.x
python sql python-3.x
edited Nov 23 '18 at 17:54
khelwood
31.3k74364
31.3k74364
asked Nov 23 '18 at 17:51
MatthiasMatthias
3518
3518
marked as duplicate by khelwood, Community♦ Nov 23 '18 at 18:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by khelwood, Community♦ Nov 23 '18 at 18:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
(var1)
isn't a tuple. It's just a variable in parentheses. Should be(var1,)
– khelwood
Nov 23 '18 at 17:53
I didn't expected that I have to use a tuple for a single value. It works and it was driving me crazy. Thank you!
– Matthias
Nov 23 '18 at 17:56
While reading again the duplicate I can confirm it would have answered my question if I had read it with the background of knowing the tuple.
– Matthias
Nov 23 '18 at 18:04
add a comment |
1
(var1)
isn't a tuple. It's just a variable in parentheses. Should be(var1,)
– khelwood
Nov 23 '18 at 17:53
I didn't expected that I have to use a tuple for a single value. It works and it was driving me crazy. Thank you!
– Matthias
Nov 23 '18 at 17:56
While reading again the duplicate I can confirm it would have answered my question if I had read it with the background of knowing the tuple.
– Matthias
Nov 23 '18 at 18:04
1
1
(var1)
isn't a tuple. It's just a variable in parentheses. Should be (var1,)
– khelwood
Nov 23 '18 at 17:53
(var1)
isn't a tuple. It's just a variable in parentheses. Should be (var1,)
– khelwood
Nov 23 '18 at 17:53
I didn't expected that I have to use a tuple for a single value. It works and it was driving me crazy. Thank you!
– Matthias
Nov 23 '18 at 17:56
I didn't expected that I have to use a tuple for a single value. It works and it was driving me crazy. Thank you!
– Matthias
Nov 23 '18 at 17:56
While reading again the duplicate I can confirm it would have answered my question if I had read it with the background of knowing the tuple.
– Matthias
Nov 23 '18 at 18:04
While reading again the duplicate I can confirm it would have answered my question if I had read it with the background of knowing the tuple.
– Matthias
Nov 23 '18 at 18:04
add a comment |
1 Answer
1
active
oldest
votes
I think you need to replace the comma with %. Python doesn’t know how to fill in %s
without an %
operator. Try:
cur.execute("INSERT INTO tblMQTTtest (string) VALUES (%s)" % var1 )
This is bad advice. You shouldn't glue variables into your SQL. The OP had it right, passing the variables separately toexecute
. xkcd.com/327
– khelwood
Nov 23 '18 at 17:58
Can you explain why this is bad advice?
– J. Behnken
Nov 23 '18 at 18:33
en.wikipedia.org/wiki/SQL_injection
– khelwood
Nov 23 '18 at 18:34
xkcd.com/327
– gilch
Nov 23 '18 at 18:43
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you need to replace the comma with %. Python doesn’t know how to fill in %s
without an %
operator. Try:
cur.execute("INSERT INTO tblMQTTtest (string) VALUES (%s)" % var1 )
This is bad advice. You shouldn't glue variables into your SQL. The OP had it right, passing the variables separately toexecute
. xkcd.com/327
– khelwood
Nov 23 '18 at 17:58
Can you explain why this is bad advice?
– J. Behnken
Nov 23 '18 at 18:33
en.wikipedia.org/wiki/SQL_injection
– khelwood
Nov 23 '18 at 18:34
xkcd.com/327
– gilch
Nov 23 '18 at 18:43
add a comment |
I think you need to replace the comma with %. Python doesn’t know how to fill in %s
without an %
operator. Try:
cur.execute("INSERT INTO tblMQTTtest (string) VALUES (%s)" % var1 )
This is bad advice. You shouldn't glue variables into your SQL. The OP had it right, passing the variables separately toexecute
. xkcd.com/327
– khelwood
Nov 23 '18 at 17:58
Can you explain why this is bad advice?
– J. Behnken
Nov 23 '18 at 18:33
en.wikipedia.org/wiki/SQL_injection
– khelwood
Nov 23 '18 at 18:34
xkcd.com/327
– gilch
Nov 23 '18 at 18:43
add a comment |
I think you need to replace the comma with %. Python doesn’t know how to fill in %s
without an %
operator. Try:
cur.execute("INSERT INTO tblMQTTtest (string) VALUES (%s)" % var1 )
I think you need to replace the comma with %. Python doesn’t know how to fill in %s
without an %
operator. Try:
cur.execute("INSERT INTO tblMQTTtest (string) VALUES (%s)" % var1 )
answered Nov 23 '18 at 17:55
J. BehnkenJ. Behnken
545
545
This is bad advice. You shouldn't glue variables into your SQL. The OP had it right, passing the variables separately toexecute
. xkcd.com/327
– khelwood
Nov 23 '18 at 17:58
Can you explain why this is bad advice?
– J. Behnken
Nov 23 '18 at 18:33
en.wikipedia.org/wiki/SQL_injection
– khelwood
Nov 23 '18 at 18:34
xkcd.com/327
– gilch
Nov 23 '18 at 18:43
add a comment |
This is bad advice. You shouldn't glue variables into your SQL. The OP had it right, passing the variables separately toexecute
. xkcd.com/327
– khelwood
Nov 23 '18 at 17:58
Can you explain why this is bad advice?
– J. Behnken
Nov 23 '18 at 18:33
en.wikipedia.org/wiki/SQL_injection
– khelwood
Nov 23 '18 at 18:34
xkcd.com/327
– gilch
Nov 23 '18 at 18:43
This is bad advice. You shouldn't glue variables into your SQL. The OP had it right, passing the variables separately to
execute
. xkcd.com/327– khelwood
Nov 23 '18 at 17:58
This is bad advice. You shouldn't glue variables into your SQL. The OP had it right, passing the variables separately to
execute
. xkcd.com/327– khelwood
Nov 23 '18 at 17:58
Can you explain why this is bad advice?
– J. Behnken
Nov 23 '18 at 18:33
Can you explain why this is bad advice?
– J. Behnken
Nov 23 '18 at 18:33
en.wikipedia.org/wiki/SQL_injection
– khelwood
Nov 23 '18 at 18:34
en.wikipedia.org/wiki/SQL_injection
– khelwood
Nov 23 '18 at 18:34
xkcd.com/327
– gilch
Nov 23 '18 at 18:43
xkcd.com/327
– gilch
Nov 23 '18 at 18:43
add a comment |
1
(var1)
isn't a tuple. It's just a variable in parentheses. Should be(var1,)
– khelwood
Nov 23 '18 at 17:53
I didn't expected that I have to use a tuple for a single value. It works and it was driving me crazy. Thank you!
– Matthias
Nov 23 '18 at 17:56
While reading again the duplicate I can confirm it would have answered my question if I had read it with the background of knowing the tuple.
– Matthias
Nov 23 '18 at 18:04