0

So I am trying to add a new entry into my mySQL Database. The problem here is, that it increases the id, but does add the entry. After a little bit of googling I found that a failed INSERT query also increases the AUTO_INCREMENTd value (id in my case).

The mySQL Table is created using CREATE TABLE IF NOT EXISTS TS3_STAMM_1 (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(64) NOT NULL, ts3_uid VARCHAR(64) NOT NULL, points INT(8) UNSIGNED NOT NULL); which is called by the function qServer.execute(querystring) inside python's MySQLdb module.

Then I use qString = "INSERT INTO TS3_STAMM_1 (name, ts3_uid, points) VALUES ('{}', '{}', {})".format(name, uid, pnts) (the datatypes are correct, I at least quadrouplechecked) with the function qServer.exectue(qString) to insert a new entry into the database.

But it is incrementing the ID, but its not adding an entry. So my guess would be its a failed query, but why? How does it happen? How to fix it?

Simple SELECT querys work fine the same way, also adding data manually works fine. Only the python query fails.

Note: qServer is the connection to the server, and its defined with:

try:
    qConn = MySQLdb.connect(host="...", user="...", passwd="...", db="...")
    qServer = qConn.cursor()
except OperationalError:
    print("Cannot connect to mySQL Database! Aborting...")
    exit(1)
2
  • 3
    Please do not use string formatting for passing arguments. That's a footgun for many reason, SQL injection being the worst. Use your DB-API driver's placeholders and pass args to execute() separately. In your incomplete example you never show a call to commit. If you do not commit, no data is persisted. Commented Jan 27, 2018 at 12:31
  • The lack of the commit() seems to have been the cause of the data not saving... Thanks! Commented Jan 27, 2018 at 13:41

1 Answer 1

2

Use commit Luke.

>>> cursor.execute("INSERT INTO employees (first_name) VALUES (%s)", ('Jane', ))
>>> qConn.commit()

Using str.format for creating SQL query is bad idea.

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

5 Comments

What is cnx in this case?
It is connection object qConn in your case.
How would that formatting approach look for more variables? I tried it with %s, %s, %d and on the %d it says %d format: a number is required, not str. This happens even when I make sure and put the pnts variable in the int() function. My approach right now is to use [...].execute("[...]", (name, uid, int(pnts))) but this cannot format on the %d since it thinks that pnts is a string, even when I mark it as int
Nevermind, I found it out. You can only format with one formatting placeholder, either %d or %s. But %s is fine, I can just convert it to a string. Thanks!
You don't need to convert to string, and int() does not mark anything, but creates a new integer. The driver creates suitable SQL for each value based on its type and in case of MySQLdb driver the placeholder is always %s, and is not Python's old style %-formatting.

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.