Python脚本之连接MySQL【四】

本文为博主原创,未经授权,严禁转载及使用。

本文链接:https://blog.csdn.net/zyooooxie/article/details/124640412

之前写了篇 Python脚本之连接MySQL【三】,日常使用过程中,代码实际有很多改动,特此更新;

【实际这篇博客推迟发布N个月】

个人博客:https://blog.csdn.net/zyooooxie

【以下所有内容仅为个人项目经历,如有不同,纯属正常】

Cursor类的nextset()、_nextset()

https://peps.python.org/pep-0249/#nextset

bash 复制代码
Cursor.nextset()

This method will make the cursor skip to the next available set, discarding any remaining rows from the current set.

If there are no more sets, the method returns None. Otherwise, it returns a true value and subsequent calls to the .fetch*() methods will return rows from the next result set.

方法优化【2】

【读取ini配置文件 + 建立、关闭数据库连接】

@filename: db.ini

bash 复制代码
[zyooooxie_db]
user = user_zy
host = host_zy
passwd = passwd_zy
database = database_zy
python 复制代码
"""
@blog: https://blog.csdn.net/zyooooxie
@qq: 153132336
@email: zyooooxie@gmail.com
"""

from pymysql.connections import Connection
from pymysql.cursors import Cursor
from pymysql.constants import CLIENT

def read_ini(ini_path: str = 'db.ini'):
    """
    读取配置文件
    :param ini_path:默认是数据库的配置文件
    :return:
    """

    if os.path.isfile(ini_path):
        config = ConfigParser()
        config.read(ini_path, encoding='UTF-8')

        return {cf: config.items(cf) for cf in config.sections()}


def connect_db(db_name: str) -> (Connection, Cursor):
    """
    建立链接,传参为 具体的db
    :param db_name:
    :return:
    """
    ini_dict = read_ini()

    if db_name in ini_dict:
        res_list = ini_dict.get(db_name)
        res_dict = dict(res_list)

    else:
        raise Exception('传参不合法')

    db = pymysql.connect(port=3306, charset='utf8', autocommit=True, client_flag=CLIENT.MULTI_STATEMENTS,
                         use_unicode=True, **res_dict)  # client_flag传值:默认可以同时执行多条sql语句的
    cur = db.cursor()

    Log.debug('{} connect'.format(db_name))

    return db, cur


def close_db(db: Connection, cur: Cursor):
    """
    断开连接
    :param db:
    :param cur:
    :return:
    """
    cur.close()
    db.close()
    Log.debug('connect close')

exe_sql()、fetch_sql()、fetch_sqls()

python 复制代码
"""
@blog: https://blog.csdn.net/zyooooxie
@qq: 153132336
@email: zyooooxie@gmail.com
"""

def exe_sqls(sql: str, db_name: str, db: Connection = None, cur: Cursor = None):
    """
    多条sql语句-execute()
    :param sql:
    :param db_name:
    :param db:
    :param cur:
    :return:
    """
    exe_sql(sql=sql, db_name=db_name, db=db, cur=cur)


def exe_sql(sql: str, db_name: str, exe_mode: str = None, data_list: list or tuple = None,
            db: Connection = None, cur: Cursor = None):
    """
    1条sql语句-execute()、executemany()
    :param sql:
    :param db_name:
    :param exe_mode:
    :param data_list:
    :param db:
    :param cur:
    :return:
    """

    if not bool(cur):
        db_use, cur_use = connect_db(db_name=db_name)

    else:
        db_use, cur_use = db, cur

    Log.info(sql)

    if data_list is None and exe_mode is None:
        try_str = """cur_use.execute(sql)"""

    elif exe_mode == 'execute' and data_list is not None:
        assert sql.find('(%s') != -1
        try_str = """cur_use.execute(sql, data_list)"""

    elif exe_mode == 'executemany' and data_list is not None:
        assert sql.find('(%s') != -1
        try_str = """cur_use.executemany(sql, data_list)"""

    else:
        Log.error('{}--{}'.format(exe_mode, data_list))
        raise Exception('Execute Error')

    try:
        result = eval(try_str, locals())

    except Exception as e:
        db_use.rollback()
        Log.error(e.args)
        Log.info(traceback.format_exc())

        result = False
        Log.error('sql执行有问题')

    else:
        execute_sql_result_check(result)

    finally:

        if not bool(cur):
            close_db(db=db_use, cur=cur_use)


def execute_sql_result_check(result):
    """
    SQL Execute结果检查
    :param result:
    :return:
    """

    if not result:
        Log.error('{}-Number of affected rows'.format(result))

    else:
        Log.debug('Execute Succeed:{}'.format(result))


def fetch_sqls(sql: str, db_name: str, db: Connection = None, cur: Cursor = None) -> List[tuple]:
    """
    多条sql语句-fetchall()
    :param sql:
    :param db_name:
    :param db:
    :param cur:
    :return:
    """
    if not bool(cur):
        db_use, cur_use = connect_db(db_name=db_name)

    else:
        db_use, cur_use = db, cur

    Log.info(sql)

    try:
        data = list()

        cur_use.execute(sql)
        data.append(cur_use.fetchall())

        # while True:
        #
        #     if cur_use.nextset():
        #         data.append(cur_use.fetchall())
        #
        #     else:
        #         break

        while cur_use.nextset():
            data.append(cur_use.fetchall())

    except Exception as e:
        db_use.rollback()

        Log.debug(e.args)
        Log.info(traceback.format_exc())

        data = False
        Log.error('sql执行有问题')

    else:

        for d in data:
            fetch_sql_result_check(d)

    finally:

        if not bool(cur):
            close_db(db=db_use, cur=cur_use)

    return data


def fetch_sql(sql: str, db_name: str, fetch_mode: str = 'fetchall', db: Connection = None, cur: Cursor = None):
    """
    1条sql语句-fetchone()、fetchall()
    :param sql:
    :param db_name:
    :param fetch_mode:
    :param db:
    :param cur:
    :return:
    """

    if not bool(cur):
        db_use, cur_use = connect_db(db_name=db_name)

    else:
        db_use, cur_use = db, cur

    Log.info(sql)

    if fetch_mode == 'fetchall':
        try_str = """cur_use.fetchall()"""

    elif fetch_mode == 'fetchone':  # 很少用到
        try_str = """cur_use.fetchone()"""

    else:
        Log.error('fetch_mode: {}'.format(fetch_mode))
        raise Exception(fetch_mode)

    try:
        cur_use.execute(sql)
        data = eval(try_str, locals())

    except Exception as e:
        db_use.rollback()

        Log.debug(e.args)
        Log.info(traceback.format_exc())

        data = False
        Log.error('sql执行有问题')

    else:
        fetch_sql_result_check(data)

    finally:

        if not bool(cur):
            close_db(db=db_use, cur=cur_use)

    return data


def fetch_sql_result_check(result):
    """
    SQL Fetch结果检查
    :param result:
    :return:
    """

    if not result:
        Log.error('{}-Fetch Nothing'.format(result))

    else:
        Log.debug('Fetch Succeed')

实际应用【2】

python 复制代码
"""
@blog: https://blog.csdn.net/zyooooxie
@qq: 153132336
@email: zyooooxie@gmail.com
"""

def test_0801():
    from XXX_use.common_mysql import fetch_sql, connect_db, close_db, fetch_sqls, exe_sql, exe_sqls

    db_, cur_ = connect_db(db_name='zy_db')

    sql = """
    SELECT  *  FROM `table_c_r`    ORDER BY `update_date` DESC ;
    """

    data = fetch_sql(sql=sql, db_name='zy_db', fetch_mode='fetchall', db=db_, cur=cur_)
    Log.info(data)

    data = fetch_sql(sql=sql, db_name='zy_db', fetch_mode='fetchone', db=db_, cur=cur_)
    Log.info(data)

    data = fetch_sql(sql=sql, db_name='zy_db', db=db_, cur=cur_)
    Log.info(data)

    sql = """
    SELECT  *  FROM `table_c_r_g_m_s`    ORDER BY `update_date` DESC LIMIT 50 ;
    
    # 这条sql查不到数据
    SELECT  *  FROM `table_c_b_r_r`  WHERE   `delete_flag` = 1   ORDER BY `update_date` DESC  ; 
    
    SELECT  *  FROM `table_c_r`    ORDER BY `update_date` DESC LIMIT 1;
    """

    res = fetch_sqls(sql=sql, db_name='zy_db', db=db_, cur=cur_)
    Log.info(res)

    data_list = [('TEST0801' + 'ZYOOOOXIE_' + str(i), 'Name' + str(i), 'zyooooxie') for i in
                 random.sample(range(500), 10)]
    Log.info(data_list)

    sql = """
    insert into `table_c_g_c`     (`c_i`, `name`, `owner`) 
    VALUES   (%s, %s, %s);
    """
    exe_sql(sql=sql, db_name='zy_db', exe_mode='executemany', data_list=data_list, db=db_, cur=cur_)

    exe_sql(sql=sql, db_name='zy_db', exe_mode='execute', data_list=data_list[-1], db=db_, cur=cur_)

    sql = """
    insert into `table_c_g_c`     (`c_i`, `name`, `owner`) 
    VALUES     ('TEST0801ZYOOOOXIE_11','Name_11', 'zyooooxie') ; 

    insert into table_c_y_a    (username, password)
    values    ('TEST0801_11', 'pwd_11');

    insert into `table_c_g_c`    (`c_i`, `name`, `owner`) 
    VALUES     ('TEST0801ZYOOOOXIE_12','Name_12', 'zyooooxie') ;    
    """
    exe_sql(sql=sql, db_name='zy_db', db=db_, cur=cur_)
    exe_sqls(sql=sql, db_name='zy_db', db=db_, cur=cur_)

    sql_ = """

    DELETE FROM `table_c_g_c` WHERE c_i LIKE 'TEST0801%' ;
    DELETE FROM `table_c_y_a` WHERE username LIKE 'TEST0801%' ;
    DELETE FROM `table_c_y_a` WHERE username LIKE 'TEST080808%' ;

    """
    exe_sql(sql=sql_, db_name='zy_db', db=db_, cur=cur_)
    exe_sqls(sql=sql_, db_name='zy_db', db=db_, cur=cur_)

    close_db(db_, cur_)

本文链接:https://blog.csdn.net/zyooooxie/article/details/124640412

个人博客 https://blog.csdn.net/zyooooxie

相关推荐
winfredzhang10 分钟前
从零构建家庭照片管理系统:Django 模块化单体实战,以及我踩到的 4 个真实坑
python·postgresql·django·sqlite·bootsrap
jyOverQ24 分钟前
LangGraph 流式执行详解:stream、astream 与 stream_mode 怎么选?
python·langchain
闲猫40 分钟前
LangGraph / Capabilities / Stores
python·agent·langgraph
pjj198541 小时前
机器学习-NumPy2
人工智能·python·机器学习
OPEN-F1 小时前
Python进阶教程:单元测试与代码质量
开发语言·python·单元测试
爱吃火鸡面呀1 小时前
MySQL 查询与函数详解:从基础条件筛选到高级字符处理
数据库·mysql
Logintern091 小时前
装饰器和洋葱模式的区分
开发语言·python·架构
知行产研2 小时前
中国矿山无人驾驶出海的三重门
数据库·mysql·php
呆呆敲代码的小Y2 小时前
10 分钟搞懂 cua:开源 AI 操作电脑基础设施,附 Python 沙箱与 Agent 上手代码
人工智能·python·开源·ai agent·awesome·llm应用·cua
工具分享2 小时前
带店托管必用爆单AI选品,一人公司轻松掌握
人工智能·python