当前位置:网站首页>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 :kpicode
2、在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获取指定的值。
最后,谢谢关注,谢谢支持!
边栏推荐
- LeetCode 312. 戳气球 每日一题
- Read PG in data warehouse in one article_ stat
- typescript ts 基础知识之类型声明
- Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images
- Binary search tree (basic operation)
- As an Android Developer programmer, Android advanced interview
- Binary search tree (features)
- 最新阿里P7技术体系,妈妈再也不用担心我找工作了
- 谎牛计数(春季每日一题 53)
- 【Seaborn】组合图表、多子图的实现
猜你喜欢
node:504报错
[designmode] facade patterns
Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
QML beginner
The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
掌握这个提升路径,面试资料分享
网关Gateway的介绍与使用
Vs2019 configuration matrix library eigen
测试用例管理工具推荐
How to add aplayer music player in blog
随机推荐
LeetCode 403. Frog crossing the river daily
[Seaborn] combination chart: facetgrid, jointgrid, pairgrid
LeetCode 1774. 最接近目标价格的甜点成本 每日一题
[medical segmentation] attention Unet
Opencv personal notes
作为Android开发程序员,android高级面试
【视频/音频数据处理】上海道宁为您带来Elecard下载、试用、教程
The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
浅谈 Apache Doris FE 处理查询 SQL 源码解析
模块六
Lowcode: four ways to help transportation companies enhance supply chain management
LeetCode 300. 最长递增子序列 每日一题
As an Android Developer programmer, Android advanced interview
typescript ts 基础知识之类型声明
Sort out several important Android knowledge and advanced Android development interview questions
Arduino 控制的双足机器人
LeetCode 1043. Separate the array to get the maximum and daily questions
深度监听 数组深度监听 watch
NeRF:DeepFake的最终替代者?
LeetCode 1981. Minimize the difference between the target value and the selected element one question per day