0

Hey Guys i'm getting errors when trying to save a value into mysql db. Here is my code.

db = MySQLdb.connect(host="localhost", user="root", passwd="developer", db="recaptcha")

cur.execute("SELECT * FROM `captcha_response`")

cur.execute("INSERT into captcha_response(id, response) values (%s, %s)" % (0, token))

Here is my error.

raise errorclass, errorvalue
OperationalError: (1054, "Unknown column '03ACgFB9t_ZvEFuHACU8HebuQ6zrFe_k0n9SLsoJU5GC3tm' in 'field list'")

Here is mysql db info.

    my database name is: "recaptcha"
    my table name is: "captcha_response"
    Example of where the items get stored
                          id                           response

Edit Copy Delete          1                            03ACgFB9uxJ...                                       

Here is the image of my table

Anthony
  • 1
  • 2
  • What columns does the captcha_response table have? You also might need a space in between the table name and the parenises for `(id,response)`. – Zack Tarr Jun 14 '18 at 06:13
  • columns are id and response – Anthony Jun 14 '18 at 06:14
  • id is basically a number that's rising as i keep adding to response – Anthony Jun 14 '18 at 06:14
  • You shouldnt have to increment ID yourself, you should be able to use the Auto Increment function in MySQL or the AI check box for the column. Then you dont insert into ID only into response, SQL then handleds the ID column for you. – Zack Tarr Jun 14 '18 at 06:16
  • so simply insert into response then – Anthony Jun 14 '18 at 06:17
  • If you make the column auto increment for you then yes. This post should be able to help you out on that change. https://stackoverflow.com/questions/5665571/auto-increment-in-phpmyadmin – Zack Tarr Jun 14 '18 at 06:18
  • made the auto infrement change. but still getting hit with the error when i do this'. – Anthony Jun 14 '18 at 06:24
  • cur.execute("INSERT into captcha_response(response) values (%s)" % (token)) – Anthony Jun 14 '18 at 06:25
  • Added an image to the post so you can see – Anthony Jun 14 '18 at 06:33
  • And you are getting the same error? And that is that not the correct value on the table in your image? Or are you expecting a different token value? – Zack Tarr Jun 14 '18 at 06:39
  • Please try and replace the `%` between your query string and the parameters tuple with a comma. – shmee Jun 14 '18 at 06:48
  • Im expecting many more values. as im getting responses i want them stored there – Anthony Jun 14 '18 at 06:49
  • @shmee okay im not getting the error anymore. but it isnt storing – Anthony Jun 14 '18 at 06:53
  • do you commit the transaction afterwards? `db.commit()` where db is the name of the variable you store your connection in. Or set your connection to autocmmit after creation and before inserting `db.autocommit = True` – shmee Jun 14 '18 at 06:55
  • Also, if you use the query as stated in the comment above with just a single value, you have to pass the parameter with a trailing comma, i.e. `(token,)` to make sure it is treated as a tuple and not as a scalar. Please see the [documentation](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html) for details – shmee Jun 14 '18 at 06:59
  • @shmee commit() worked! thank you so much – Anthony Jun 14 '18 at 07:01

1 Answers1

0

Here was the answer guys! thanks for the help all!

db = MySQLdb.connect(host="localhost", user="root", passwd="developer", db="recaptcha")

cur.execute("SELECT * FROM `captcha_response`")

cur.execute("INSERT into captcha_response(id, response) values (%s, %s)", (0, token))

db.commit()
Anthony
  • 1
  • 2