Home > Back-end >  getting error - ERROR in app: Exception on /delete [POST], while trying to delete a record from mysq
getting error - ERROR in app: Exception on /delete [POST], while trying to delete a record from mysq

Time:08-21

Here is the python script

@app.route("/delete", methods=['POST'])
def delete_record():
 mydb = con.connect(host="localhost", user="root", password="password", database="mysql_python")
 cursor = mydb.cursor()

 first_name = request.json['firstname']
 query = "delete from user_accounts where First_Name = %s"

 try:
     cursor.execute(query, first_name)
     mydb.commit()
     mydb.close()
     return "success"
 except Exception as e:
     return e

Here is json script passing from postman

{
    "firstname":"Vikrant"
}

i am new to postman, trying to test the api through http://127.0.0.1:5000/delete using POST method. Not getting what i am doing wrong.

Here is full error

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL C to quit
[2022-08-21 11:43:41,281] ERROR in app: Exception on /delete [POST]
Traceback (most recent call last):
  File "C:\Users\abhi\PycharmProjects\api\venv\lib\site-packages\flask\app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\abhi\PycharmProjects\api\venv\lib\site-packages\flask\app.py", line 1823, in full_dispatch_request
    return self.finalize_request(rv)
  File "C:\Users\abhi\PycharmProjects\api\venv\lib\site-packages\flask\app.py", line 1842, in finalize_request
    response = self.make_response(rv)
  File "C:\Users\abhi\PycharmProjects\api\venv\lib\site-packages\flask\app.py", line 2170, in make_response
    raise TypeError(
TypeError: The view function did not return a valid response. The return type must be a string, dict, list, tuple with headers or status, Response instance, or WSGI callable, but it was a ProgrammingError.
127.0.0.1 - - [21/Aug/2022 11:43:41] "POST /delete HTTP/1.1" 500 -

Error on postman

<!doctype html>
<html lang=en>
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or
    there is an error in the application.</p>

CodePudding user response:

The development server always shows this warning. The development server is not intended for use in production. It is not designed to be particularly efficient, stable, or secure. Use a production WSGI server instead. See the deployment docs from Flask for more information.

That warning is just a warning though, it's not an error preventing your app from running. If your app isn't working, there's something else wrong with your code.

CodePudding user response:

Convert the error message to any one of the acceptable formats like string or list or tuple or dict and return it. That will solve your issue.

For permanent solution check your delete cursor and fix it

CodePudding user response:

Minor change has resolved the error, which was - parameter need to be pass as list, tuple or dict as the error suggest so i passed it as

cursor.execute(query, [first_name])

which was earlier

cursor.execute(query, first_name)
  • Related