简体   繁体   English

评论使用 cursor object 和 pymysql

[英]Comment on using cursor object and pymysql

In my function I access DB like this:在我的 function 中,我像这样访问数据库:

 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?在正文中创建了两个游标,最后,您要关闭哪一个,它们具有不同的 memory 位置? You could put “try” after cursor = self.__db.cursor(pymysql.cursors.DictCursor)您可以在 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?我在这里检查: https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/tests/test_DictCursor.py在这里: pymysql fetchall() 结果为字典? they both have the same way of using it.他们都有相同的使用方式。 Please give your advise.请给出你的建议。 Thanks.谢谢。

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

`self.__con.checkConnectionStatus() is a redundant check. `self.__con.checkConnectionStatus() 是一个冗余检查。 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.如果上面第二行的执行失败,则会引发异常,并且将执行exceptfinally块。 The line itself is a check for the connection status, which is what makes it redundant.该线路本身是对连接状态的检查,这就是使其冗余的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM