当前位置:网站首页>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获取指定的值。
最后,谢谢关注,谢谢支持!
边栏推荐
- [PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
- 测试用例管理工具推荐
- QT视频传输
- 直接上干货,100%好评
- LeetCode 1155. N ways to roll dice one question per day
- LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
- mysql实现两个字段合并成一个字段查询
- 谈谈 SAP 系统的权限管控和事务记录功能的实现
- null == undefined
- LeetCode 1654. 到家的最少跳跃次数 每日一题
猜你喜欢

Binary search tree (basic operation)
作为Android开发程序员,android高级面试

Lowcode: four ways to help transportation companies enhance supply chain management

Test case management tool recommendation

QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏

掌握这个提升路径,面试资料分享

科普达人丨一文弄懂什么是云计算?

Interface oriented programming

值得一看,面试考点与面试技巧

测试用例管理工具推荐
随机推荐
低代码(lowcode)帮助运输公司增强供应链管理的4种方式
node:504报错
Binary search tree (features)
[medical segmentation] attention Unet
Number of exchanges in the 9th Blue Bridge Cup finals
[designmode] flyweight pattern
LeetCode 312. 戳气球 每日一题
Master this promotion path and share interview materials
LeetCode 1049. Weight of the last stone II daily question
ORACLE进阶(六)ORACLE expdp/impdp详解
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
skimage学习(1)
LeetCode 1774. The dessert cost closest to the target price is one question per day
打造All-in-One应用开发平台,轻流树立无代码行业标杆
【Seaborn】组合图表、多子图的实现
防火墙系统崩溃、文件丢失的修复方法,材料成本0元
os、sys、random标准库主要功能
两类更新丢失及解决办法
LeetCode 300. Daily question of the longest increasing subsequence
LeetCode 213. 打家劫舍 II 每日一题