In my function I access DB like this :
def send_message(self, _name, _email, _message):
if _name is None:
custom_resp = {
"message": "Error: No name provided",
"status": 400
}
resp = jsonify(custom_resp)
return resp
if _email is None:
custom_resp = {
"message": "Error: No email provided",
"status": 400
}
resp = jsonify(custom_resp)
return resp
if _message is None:
custom_resp = {
"message": "Error: No message provided",
"status": 400
}
resp = jsonify(custom_resp)
return resp
try:
if _name and _email and _message:
# save edits
sql = "INSERT INTO `contact`(`name`, `email`, `message`) VALUES(%s, %s, %s)"
data = (_name, _email, _message)
self.__con.checkConnectionStatus()
cursor = self.__db.cursor(pymysql.cursors.DictCursor)
cursor.execute(sql, data)
self.__db.commit()
if cursor.rowcount > 0:
custom_response = {
'status': 200,
'message': 'success',
}
resp = custom_response
return resp, 200
else:
custom_resp = {
"message": "Error: Could not proceed your request",
"status": 400
}
resp = jsonify(custom_resp)
return resp
except Exception as e:
print(e)
finally:
if self.__con is not None:
self.__con.closeConnection()
This code can run totally well for me. But I got a comment that I:
created two cursors in body and finally, which one you want to close, they have different memory locations? You could put “try” after cursor = self.__db.cursor(pymysql.cursors.DictCursor)
self.__con.checkConnectionStatus()
cursor = self.__db.cursor(pymysql.cursors.DictCursor)
Is it true? I don't see any problem actually and this comment may be invalid. I checked here: https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/tests/test_DictCursor.py and here: pymysql fetchall() results as dictionary? they both have the same way of using it. Please give your advise ! Thanks.