• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

python 通过yaml文件连接mysql

武飞扬头像
qq_08110123
帮助1

背景:

python 连接mysql数据库,一般简单的参数写死方式如下:

db = pymysql.connect(host='127.0.0.1',port=3306,user='root',password='password123',database='learning',charset='utf8')

有时候需要连接不同的数据库地址,在代码中直接修改,容易出错。数据库连接参数一般不会变动,所以我们可以选择写成配置文件。这里介绍下,如何通过写yaml配置文件连接mysql。

思路:

  • 将参数写入yaml文件

  • 创建类和方法读取yaml文件

  • 连接数据库,调用方法传入参数

详细步骤:

  1. 创建mysqlConnect.yaml 文件,写入数据如下

  1.  
    # 数据库配置
  2.  
     
  3.  
    # 键值对格式 dev:{host:127.0.0.1,port:3306,user:root,password:password123,database:learning,charset:utf8}
  4.  
    dev:
  5.  
    host: 127.0.0.1
  6.  
    port: 3306
  7.  
    user: root
  8.  
    password: password123
  9.  
    database: learning
  10.  
    charset: utf8
  11.  
     
  12.  
    prod:
  13.  
    host: 127.0.0.1
  14.  
    port: 3307
  15.  
    user: root
  16.  
    password: password123
  17.  
    database: learning-prod
  18.  
    charset: utf8
学新通
  1. 创建handle_yaml.py文件,创建类和方法读取yaml文件

  1.  
    import yaml
  2.  
    class MysqlConnectYaml: # 操作mysqlConnect.yaml文件的
  3.  
    def get_yaml_mysql(self,file_path):
  4.  
    file = open(file_path,'r',encoding='utf-8
  5.  
    res = yaml.safe_load(file)
  6.  
    file.close()
  7.  
    return res
  8.  
     
  9.  
    def get_dev(self, env, file_path):
  10.  
    resp = self.get_yaml_mysql(file_path)
  11.  
    print(type(resp)) # 是字典格式
  12.  
    if env in "prod":
  13.  
    return resp["prod"]
  14.  
    else:
  15.  
    return resp["dev"]
  16.  
     
  17.  
    ""
  18.  
    if __name__ == '__main__':
  19.  
     
  20.  
    db_yaml = MysqlConnectYaml()
  21.  
    db_connect = db_yaml.get_dev("dev","../data/mysqlConnect.yaml") # 返回是字典格式,所以直接下标取数据
  22.  
    print(type(db_yaml), type(db_connect))
  23.  
    print(db_connect)
  24.  
    print(db_connect["port"])
学新通

输出结果

  1.  
    <class 'dict'>
  2.  
    <class '__main__.MysqlConnectYaml'> <class 'dict'>
  3.  
    {'host': '127.0.0.1', 'port': 3306, 'user': 'root', 'password': 'password123', 'database': 'learning', 'charset': 'utf8'}
  4.  
    3306
  1. 创建handle_mysql.py文件,调用相关方法

  1.  
    import pymysql
  2.  
    from utils.handle_yaml import MysqlConnectYaml
  3.  
     
  4.  
    class MysqlConnections:
  5.  
     
  6.  
    # 方法二:通过yaml配置
  7.  
    def __init__(self,env,file_path):
  8.  
    self.db_yaml = MysqlConnectYaml() # 实例化
  9.  
    self.db_connect = self.db_yaml.get_dev(env,file_path)
  10.  
     
  11.  
            # 读取配置参数
  12.  
    self.db = pymysql.Connect(host=self.db_connect["host"],port=self.db_connect["port"],user=self.db_connect["user"],password=self.db_connect["password"],database=self.db_connect["database"],charset=self.db_connect["charset"])
  13.  
    self.cursor = self.db.cursor()
  14.  
     
  15.  
    if __name__ == '__main__':
  16.  
    db = MysqlConnections("dev", "../data/mysqlConnect.yaml")
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcfgek
系列文章
更多 icon
同类精品
更多 icon
继续加载