当前位置:网站首页>Flask build API service SQL configuration file

Flask build API service SQL configuration file

2022-07-07 17:08:00 Python and big data analysis

Continued (Flask build api service ) hold SQL Statements are stored in a dictionary , Configuration and code are not separated , I have been struggling with the problem of how to deal with multi line configuration items in the configuration file , For a long time , In fact, this problem does not exist , Just add a space in front of the cross line part in the configuration item .

ini Is a traditional mainstream configuration file .

ini The supported data types are limited , Default all values to string ( There is no need to add quotation marks to the outside of the string ).

ini The configuration file must use [](section) Grouping , Each key is called option.

1、 Append configuration file SQL.ini

[KPIDB]
sql1 = select a.*
         from kpi_value a
        where a.kpicode in ('01010101','02010101','03010101')
          and a.datelevel='01'
          and a.regionlevel='02'
sql2 = select a.*
         from kpi_value a
        where a.kpicode in ('01010101','02010101','03010101')
          and a.datelevel='01'
          and a.regionlevel='02'
          and a.datecode>=:begindate and a.datecode<=:enddate
sql3 = select a.*
         from kpi_value a
          and a.datelevel='01'
          and a.regionlevel='02'
          and a.datecode>=:begindate and a.datecode<=:enddate
          and a.kpicode in :kpicode

2、 stay app.py Add a configparser Read

import configparser
config = configparser.ConfigParser()
config.read('SQL.ini',encoding="utf-8-sig")
DBSECTION='KPIDB'

3、 Use in each routing function config Item replaces the original dictionary method

# sqltext = sqldict[sqlid]
sqltext = config[DBSECTION][sqlid]

pycharm Read ini The configuration file , Use native configparser modular .

The specific steps are as follows :

1、 Import ConfigParser class ;

2、 Instantiation ConfigParser;

3、 Use read() Method to open and read the contents of the file ;

4、 Use get() Method , according to section and option Gets the specified value .

Last , Thank you for your attention , Thank you for your support !

原网站

版权声明
本文为[Python and big data analysis]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071532458046.html