繁体   English   中英

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

[英]How to execute a MySQL query with a python script using the MySQLdb library?

我试图修改一个 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

根据文档,您首先需要在连接打开后创建一个游标,因为“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()

暂无
暂无

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

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