当前位置:网站首页>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获取指定的值。
最后,谢谢关注,谢谢支持!
边栏推荐
- LeetCode 1031. Maximum sum of two non overlapping subarrays
- 整理几个重要的Android知识,高级Android开发面试题
- Temperature sensor chip used in temperature detector
- Pisa-Proxy SQL 解析之 Lex & Yacc
- 99% 用户在 Power BI 云端报表常犯错误
- Test case management tool recommendation
- 打造All-in-One应用开发平台,轻流树立无代码行业标杆
- As an Android Developer programmer, Android advanced interview
- 如何选择合适的自动化测试工具?
- Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
猜你喜欢

Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions

NeRF:DeepFake的最终替代者?

skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值

How to add aplayer music player in blog

Skimage learning (2) -- RGB to grayscale, RGB to HSV, histogram matching

【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid
![[designmode] facade patterns](/img/79/cde2c18e2ec8b08697662ac352ff90.png)
[designmode] facade patterns

Test case management tool recommendation

time标准库

SIGGRAPH 2022最佳技术论文奖重磅出炉!北大陈宝权团队获荣誉提名
随机推荐
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
LeetCode 1155. N ways to roll dice one question per day
Talk about the realization of authority control and transaction record function of SAP system
Deep listening array deep listening watch
LeetCode 213. 打家劫舍 II 每日一题
LeetCode 1043. 分隔数组以得到最大和 每日一题
[designmode] proxy pattern
A tour of gRPC:03 - proto序列化/反序列化
LeetCode 1986. 完成任务的最少工作时间段 每日一题
Ray and OBB intersection detection
skimage学习(1)
time标准库
QT video transmission
浅谈 Apache Doris FE 处理查询 SQL 源码解析
LeetCode 1696. Jumping game VI daily question
OpenGL personal notes
谈谈 SAP 系统的权限管控和事务记录功能的实现
Lie cow count (spring daily question 53)
Skimage learning (1)
typescript ts 基础知识之类型声明