微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 MySQLdb 库使用 python 脚本执行 MySQL 查询?

如何解决如何使用 MySQLdb 库使用 python 脚本执行 MySQL 查询?

我试图修改一个 ETL,但我发现老开发人员直接在连接上执行他的命令(ETL 已经运行了几年)。当我自己尝试执行此操作时,出现错误(因为我的编译器希望我从游标执行此操作)。

from etl.utils.logging import info
from etl.MysqL.connect import db,db_name
from etl.MysqL.operations import add_column_if_not_exists
from etl.utils.array import chunks
from pprint import pprint


def add_column_exclude_from_statistics():
    with db as c:
        # Create new columns where exclude_from_statistics
        info("Creating column exclude from statistics")
        c.execute("""
            UPDATE orders
            INNER JOIN contacts ON orders.id = contacts.`Contact ID`
            IF contacts.`Great Benefactor` = true OR orders.Campaign = `nuit-pour-la-mission` 
                SET orders.exclude_from_statistics = 1
            ELSE 
                SET orders.exclude_from_statistics = 0
            ;
        """)

def main():
    info("Table crm.orders")
    add_column_exclude_from_statistics()


if __name__ == '__main__':
    main()

但它返回'Connection' object has no attribute 'execute'

(venv) C:\Users\antoi\Documents\Programming\Work\data-tools>py -m etl.task.crm_orders_exclude_from_statistics
2021-06-25 17:12:44.357297 - Connecting to database hozana_data...
2021-06-25 17:12:44.365267 - Connecting to archive database hozana_archive...
2021-06-25 17:12:44.365267 - Table crm.orders
2021-06-25 17:12:44.365267 - Creating column exclude from statistics
Traceback (most recent call last):
  File "C:\Users\antoi\AppData\Local\Programs\Python\python39\lib\runpy.py",line 197,in _run_module_as_main
    return _run_code(code,main_globals,None,File "C:\Users\antoi\AppData\Local\Programs\Python\python39\lib\runpy.py",line 87,in _run_code
    exec(code,run_globals)
  File "C:\Users\antoi\Documents\Programming\Work\data-tools\etl\task\crm_orders_exclude_from_statistics.py",line 28,in <module>
    main()
  File "C:\Users\antoi\Documents\Programming\Work\data-tools\etl\task\crm_orders_exclude_from_statistics.py",line 24,in main
    add_column_exclude_from_statistics()
  File "C:\Users\antoi\Documents\Programming\Work\data-tools\etl\task\crm_orders_exclude_from_statistics.py",line 12,in add_column_exclude_from_statistic
s
    c.execute("""
AttributeError: 'Connection' object has no attribute 'execute'

这是我们在 etl.MysqL.connect 中的内容

import os
import MysqLdb

from etl.utils.logging import info

db_host = os.environ['DB_HOST']
db_port = int(os.environ['DB_PORT'])
db_user = os.environ['DB_USER']
db_password = os.environ['DB_PASSWORD']
db_name = os.environ['DB_NAME']
db_name_archive = os.environ['DB_ARCHIVE_NAME']

info("Connecting to database {}...".format(db_name))
db = MysqLdb.connect(host=db_host,port=db_port,db=db_name,user=db_user,passwd=db_password)

这样做很奇怪,不是吗?是不是我的 MysqLdb 库不是最新的?

这里是 MysqL 相关的库。我没有找到 MysqLdb:

(venv) C:\Users\antoi\Documents\Programming\Work\data-tools>pip list |findstr MysqL
MysqL                    0.0.3
mysql-connector-python   8.0.25
MysqLclient              2.0.3

解决方法

根据 documentation,您首先需要在连接打开后创建一个游标,因为“Connection”对象没有执行方法,但 Cursor 有,因此使用您的代码示例:

from etl.utils.logging import info
from etl.mysql.connect import db,db_name
from etl.mysql.operations import add_column_if_not_exists
from etl.utils.array import chunks
from pprint import pprint


def add_column_exclude_from_statistics():
    with db as c:
        # Create new columns where exclude_from_statistics
        info("Creating column exclude from statistics")
        cursor = c.cursor() #Get the cursor
        cursor.execute("""
            UPDATE orders
            INNER JOIN contacts ON orders.id = contacts.`Contact ID`
            IF contacts.`Great Benefactor` = true OR orders.Campaign = `nuit-pour-la-mission` 
                SET orders.exclude_from_statistics = 1
            ELSE 
                SET orders.exclude_from_statistics = 0
            ;
        """)

def main():
    info("Table crm.orders")
    add_column_exclude_from_statistics()


if __name__ == '__main__':
    main()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?