美文网首页python学习接口测试
python 读取配置文件连接数据库

python 读取配置文件连接数据库

作者: freedom_smile | 来源:发表于2020-11-02 14:16 被阅读0次

config.ini 配置文件内容:

[DATABASE]
host = 192.168.19.111
username = user
password = pass
port = 4333
database = club_ssss
charset = utf8

读取配置文件的内容:

import configparser
import base64
from  A_shop.utils.get_path import get_file_path

class ReadConfig:
    def __init__(self):
        file_path = get_file_path("config","config.ini")
        self.conf = configparser.ConfigParser()
        self.conf.read(file_path,encoding="utf8")

    def get_config_sections(self):
        return self.conf.sections()

    def get_config_options(self, section):
        return self.conf.options(section)

    def get_config_items(self, section):
        return self.conf.items(section)

    def get_config_str(self, section, option):
        return self.conf.get(section, option)

    def get_config_boolean(self, section, option):
        return self.conf.getboolean(section, option)

    def get_config_int(self, section, option):
        return self.conf.getint(section, option)

    def get_config_float(self, section, option):
        return self.conf.getfloat(section, option)

if __name__ == '__main__':
    rc = ReadConfig()
    print(rc.get_config_sections())
    print(rc.get_config_options('DATABASE'))
    print(rc.get_config_items('DATABASE'))
    print(rc.get_config_str('DATABASE', 'host'))
    data_base = rc.get_config_int("DATABASE","port")
    print(type(data_base))
    print(data_base)
返回结果:
['DATABASE']
['host', 'username', 'password', 'port', 'database', 'charset']
[('host', '192.168.19.111'), ('username', 'user'), ('password', 'pass'), ('port', '4333'), ('database', 'club_ssss'), ('charset', 'utf8')]
192.168.19.111
<class 'int'>
4333
公用的获取路径的方法:
def get_file_path(path,file):
    project_name = "A_shop"
    curPath = os.path.abspath(os.path.dirname(__file__))
    rootPath = curPath[:curPath.find(project_name + "\\") + len(project_name + "\\")]  
    return rootPath + path + '\\' + file

python源码连接数据库,得到想要的数据

from A_shop.utils.read_config import ReadConfig
import pandas as pd
from sqlalchemy import create_engine

def get_mysql_data_pd(rc,sql):
    # MySQL的用户名:user, 密码:pass, 端口:4333,数据库:club_ssss
    # connect_info = 'mysql+pymysql://user:pass@192.168.19.219:4333/club_ssss?charset=utf8'
    # engine = create_engine('mysql+pymysql://user:pass@192.168.19.219:4333:4333/club_ssss')

    connect_info = 'mysql+pymysql://{username}:{password}@{host}:{port}/{db}?charset=utf8'.format(
        username = rc.get_config_str('DATABASE', 'username'),
        password = rc.get_config_str('DATABASE', 'password'),
        host = rc.get_config_str('DATABASE', 'host'),
        port = rc.get_config_str('DATABASE', 'port'),
        db = 'club_shop')

    engine = create_engine(connect_info)
    df = pd.read_sql_query(sql, engine)
    # super_merchant_code = df['super_merchant_code']
    # merchant_code = df['merchant_code']
    # order_no = df['order_no']
    return df

if __name__ == '__main__':
    # 读取配置文件的数据库参数
    rc = ReadConfig()

    # 查询语句,选出订单表中需要的所有数据
    sql = '''SELECT so.super_merchant_code,so.merchant_code,so.order_no
                FROM `shop_order_2020_10_12` so
                WHERE so.`super_merchant_code`='EW_N2012672104'
                AND so.order_status = 1
                AND so.pay_type IN(0,1)
                AND so.pay_status = 1'''

    df = get_mysql_data_pd(rc,sql)

   # 打印看效果
    for i in range(len(df)):
        print('{} super_merchant_code: {}'.format(i,df['super_merchant_code'][i]))
        print('{} merchant_code: {}'.format(i,df['merchant_code'][i],i))
        print('{} order_no: {}'.format(i,df['order_no'][i]))
返回结果:
0 super_merchant_code: EW_N2012672111
0 merchant_code: EW_N2850192333
0 order_no: 20201012101342444
1 super_merchant_code: EW_N201267211
1 merchant_code: EW_N2850192333
1 order_no: 2020101210193097279123
2 super_merchant_code: EW_N2012672111
2 merchant_code: EW_N2850192333
2 order_no: 20201012142445112

相关文章

网友评论

    本文标题:python 读取配置文件连接数据库

    本文链接:https://www.haomeiwen.com/subject/ziipvktx.html