当前位置:网站首页>Flask搭建api服务-SQL配置文件
Flask搭建api服务-SQL配置文件
2022-07-07 15:34:00 【python与大数据分析】
续前文(Flask搭建api服务)把SQL语句以字典的方式存储起来,配置和代码没有分割开来,一直纠结如何处理配置文件中多行配置项的问题,查了半天,其实这个问题不存在的,只要在配置项中跨行部分前面追加一个空格即可。
ini是传统的主流配置文件。
ini支持的数据类型有限,将所有的值都默认成字符串(字符串最外面不需要添加引号)。
ini配置文件必须使用[](section)进行分组,每一个键称为option。
1、追加配置文件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 :kpicode2、在app.py中追加一个configparser读取
import configparser
config = configparser.ConfigParser()
config.read('SQL.ini',encoding="utf-8-sig")
DBSECTION='KPIDB'3、在各个路由函数中使用config项替代原来的字典方式
# sqltext = sqldict[sqlid]
sqltext = config[DBSECTION][sqlid]pycharm读取ini配置文件,使用自带的configparser模块。
具体步骤如下:
1、导入ConfigParser类;
2、实例化ConfigParser;
3、使用read()方法打开并读取文件内容;
4、使用get()方法,根据section和option获取指定的值。
最后,谢谢关注,谢谢支持!
边栏推荐
猜你喜欢
随机推荐
作为Android开发程序员,android高级面试
[medical segmentation] attention Unet
Build an all in one application development platform, light flow, and establish a code free industry benchmark
掌握这个提升路径,面试资料分享
QT video transmission
【Seaborn】组合图表、多子图的实现
最新阿里P7技术体系,妈妈再也不用担心我找工作了
LeetCode 300. Daily question of the longest increasing subsequence
LeetCode 1043. 分隔数组以得到最大和 每日一题
打造All-in-One应用开发平台,轻流树立无代码行业标杆
最新Android高级面试题汇总,Android面试题及答案
dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
字节跳动Android面试,知识点总结+面试题解析
LeetCode 1155. 掷骰子的N种方法 每日一题
Master this promotion path and share interview materials
ORACLE进阶(六)ORACLE expdp/impdp详解
Skimage learning (3) -- adapt the gray filter to RGB images, separate colors by immunohistochemical staining, and filter the maximum value of the region
LeetCode 403. Frog crossing the river daily
【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid
LeetCode 1774. 最接近目标价格的甜点成本 每日一题









