1

I have a Python script that inserts data into a MySQL database, but to preserve the id values I want it so that if it finds the same name then it will just update the vuln_count column for that record.

I have the following MySQL insert code:

cur.execute("INSERT INTO vuln_sets (vulntype, displayname, bulletinfamily, vulncount)"
                                " values (%s,%s,%s,%s,)", [k, vuln_name, vuln_bulletinfamily, vuln_count], "ON DUPLICATE KEY UPDATE vulncount = values(%s)", [vuln_count])

But I have an error of execute() takes from 2 to 3 positional arguments but 5 were given

1
  • That's not how this works... Commented Oct 23, 2017 at 17:00

1 Answer 1

1

That's not how execute works: it takes two explicit parameters: the query, and the parameters.

You can not write a part of the query followed by parameters followed by another part of the query and so on.

So you simply write it as a single query, and join the two lists of parameters together:

cur.execute(
    "INSERT INTO vuln_sets (vulntype, displayname, bulletinfamily, vulncount)"
    " VALUES (%s,%s,%s,%s) ON DUPLICATE KEY"
    " UPDATE vulncount = %s",
    [k, vuln_name, vuln_bulletinfamily, vuln_count, vuln_count]
)
Sign up to request clarification or add additional context in comments.

11 Comments

this just gives an error of: 1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') ON DUPLICATE KEY UPDATE vulncount = VALUES (1765)' at line 1")
@Luke: What if you remove the comma (edited answer).
@Luke: did you actually tested your query before you posted. Beause ON DUPLCATE KEY UPDATE vulncount seems illegal syntax. Third attempt.
@Luke: I edited the answer by what I think is valid MySQL.
@Luke: well I think we make progress, since it is a different error. New attempt.
|

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.