1
        sql = ("INSERT INTO {0} "
               "(id, timestamp, status, priority, client, group, capacity, level, mail_id) "
               "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)".format(TABLE_NAME_MAIL))
        values = ('NULL', report['timestamp'], 'succeeded', report['priority'], c.strip(), report['group'], 'NULL', 'NULL', ref_mailid)
        cursor.execute(sql, values)
        #cursor.execute('INSERT INTO %s VALUES (NULL,"%s","%s","%s","%s","%s",NULL,NULL,"%s") ' % (TABLE_NAME_REPORT, report['timestamp'], 'succeeded', report['priority'], c.strip(), report['group'], ref_mailid))

The commented out cursor.execute works, the uncommented throws an error:

_mysql_exceptions.ProgrammingError: (1064, "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 'group, capacity, level, mail_id) VALUES ('NULL', '2014-12-05 23:46:56', 'succeed' at line 1")

Column 'id' has AUTO_INCREMENT

Why am I getting this error?

2 Answers 2

5

group is a reserved keyword in MySQL. Use backticks to escape the name

INSERT INTO {0} (id, timestamp, status, priority, client, `group`, capacity ...
                      here--------------------------------^-----^

or even better use a different column name.

And don't use 'NULL' as parameter. Either use NULL without the quotes or remove it completely from the query:

INSERT INTO {0} (timestamp, ...
                ^----------no ID
Sign up to request clarification or add additional context in comments.

1 Comment

Vielen Dank Jürgen, es hat geklappt! Actually, to add I use None instead of NULL.
0

When using an auto increment column, you have two options:

  1. Insert a value in there (ie: Override the auto increment)
  2. Do not set the column at all

1:

In case of setting a value, and you want to set "NULL" the column has to allow a NULL. This is rarely the case with an auto increment column since it would defeat its purpose.

So this is allowed:

INSERT INTO tab (id, sometext) VALUES (10,"Hi");

2:

Alternative (most used) is not to name the column at all:

INSERT INTO tab (sometext) VALUES ("Hi");

which would lead to mysql assigning a value to id in your table.

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.