0
curs.execute ("INSERT INTO temperatur (datum, uhrzeit, ort, messwert) VALUES (CURRENT_DATE(), NOW(), %s, %s);" % ort, messwert)

drops out this->

    curs.execute ("INSERT INTO temperatur (datum, uhrzeit, ort, messwert) VALUES (CURRENT_DATE(), NOW(), %s, %s);" % ort, messwert)
TypeError: not enough arguments for format string

why is this not working?

5
  • 2
    Don't use string formatting to create SQL queries; it's an unnecessary injection risk, let the engine do it for you. In this case, the problem is simply that messwert is a second parameter to execute, not a second value to be formatted into the string. Commented Feb 5, 2018 at 21:00
  • is this pymysql? Commented Feb 5, 2018 at 21:01
  • 1
    Never perform parameter formatting in SQL yourself, it makes it very vulnerable to SQL injection. Commented Feb 5, 2018 at 21:01
  • its MySQLdb. how does the engine this for me? Commented Feb 5, 2018 at 21:02
  • @KaiH.: also see Python MySQL Parameterized Queries Commented Feb 5, 2018 at 21:04

2 Answers 2

3

Do not use string formatting, you are exposing yourself to SQL injection.

Pass in your arguments as a sequence in the second argument:

curs.execute(
    "INSERT INTO temperatur (datum, uhrzeit, ort, messwert) "
    "VALUES (CURRENT_DATE(), NOW(), %s, %s)",
    (ort, messwert))

Here (ord, messwert) is a tuple passed in as the second argument to curs.execute(). You don't need the ; in the SQL statement.

You got the error because you only passed one value to the % string format, the expression you used is "string value" % ort, as messwert was being passed in as the second argument to curs.execute(). You'd have to use "string value" % (ord, messwert) instead. However, you should avoid using string formatting altogether however, as that leaves you vulnerable to SQL injection attacks where an attacker makes use of the lack of proper escaping applied to the values interpolated into the SQL string.

Sign up to request clarification or add additional context in comments.

Comments

0

Everyone is right that you shouldn't use string formatting, but the quick answer for you is just that you need parentheses containing your tuple, i.e., %(ort, messwert)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.