python - mysql.connector.connect和connection.cursor的成本

标签 python mysql mysql-connector

我是 Python 新手。我必须使用 Python mysql 驱动程序 mysql.connector 在 MySQL 中运行查询。我想知道的是打开连接和获取光标的相对成本,因为我需要在循环中执行此操作。目前,我正在循环外打开连接并将光标置于循环内:

def example(self, array):
        cnx = mysql.connector.connect()

        for item in array:
            cursor = cnx.cursor()  

            query = ("dummy query")
            cursor.execute(query)

            result = cursor.fetchall()
            # do something here...

            cursor.close()

        cnx.close()

我想知道cnx.cursor()是否是一个昂贵的操作。谢谢。

最佳答案

连接很昂贵,但游标却不是。你的做法是正确的。

关于python - mysql.connector.connect和connection.cursor的成本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47769088/

相关文章:

python - 在列上重复一维数组

mysql - MySQL View 中的 BLOB 而不是正确的数据

python - 如何点击单选按钮(我读过以前的问题)python?

mysql - Wordpress $wpdb->get_results 只返回一行

mysql - 返回更多属性作为响应(来自数据库模型外部)

java - GlassFish 中的 mysql 连接器有问题还是只有我?

ms-access - ODBC 机器 DSN 设置存储在 Windows 中的什么位置?

mysql - 启动 spark sql 和 thrift server 时在类路径上找不到 com.mysql.jdbc.Driver

Python - 将类作为参数传递给该类中方法的装饰器?

python - 无法迭代列表 -pyPDF2