0

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.

user1314404
  • 1,253
  • 3
  • 22
  • 51

1 Answers1

1
self.__con.checkConnectionStatus()
    cursor = self.__db.cursor(pymysql.cursors.DictCursor) #cursor1

`self.__con.checkConnectionStatus() is a redundant check. You can remove that line form your code.

# self.__con.checkConnectionStatus() -> remove this
cursor = self.__db.cursor(pymysql.cursors.DictCursor) #cursor2

If the execution of second line above fails, there will be an exception raised and except and finally blocks would execute. The line itself is a check for the connection status, which is what makes it redundant.

gpk27
  • 809
  • 2
  • 8
  • 19
  • yes, it is here: self.__db = MysqlConnector().getConnectorInstance(); self.__con = MysqlConnector() – user1314404 Nov 20 '19 at 08:57
  • I see people can write this, is there any difference: cur = self.conn.cursor(MyDictCursor); cur.execute("SELECT * FROM dictcursor WHERE name='bob'") – user1314404 Nov 20 '19 at 08:59
  • I have personally used the second way you mentioned so I am unsure about the first way. – gpk27 Nov 20 '19 at 09:43