0

I am creating a python API that will take values from a user in the form of a JSON object and insert these values into a database. Instead of inserting the data, a 0 is being inserted while the id increments as expected. Would anyone be able to advise me as to how to solve this problem?

cursor = cnx.cursor()

sql = "INSERT INTO `player`(`player_id`,`Name`, `Age`, `Overall`, `Position`) " \
      "VALUES ('', `Name`=%s,`Age`=%s,`Overall`=%s,`Position`=%s);"

cursor.execute(sql, (request.json['Name'], request.json['Age'],
                     request.json['Overall'], request.json['Position']))

cnx.commit()

The code above is what I am currently using. All help is appreciated

2
  • 0 is inserted where? Every value?
    – roganjosh
    Dec 9, 2018 at 21:15
  • Yes, every value apart from player_id which auto increments
    – M.Walker
    Dec 9, 2018 at 21:18

1 Answer 1

0

Use NULL for auto incrementing fields, and don't specify the name of the field in the VALUES below:

cursor = cnx.cursor()

sql = "INSERT INTO `player`(`player_id`,`Name`, `Age`, `Overall`, `Position`) " \
      "VALUES (NULL, %s, %s, %s, %s);"

cursor.execute(sql, (request.json['Name'], request.json['Age'],
                     request.json['Overall'], request.json['Position']))

cnx.commit()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.