sql的简单方法
simqle的Python项目详细描述
simqle
The simple way to SQL
非常适合在python项目中使用不繁琐的sql。执行sql并返回simple 带有命名参数的记录集。管理多个连接,并切换 在生产、开发和测试模式之间。
安装
储存库
https://github.com/Harlekuin/SimQLe
或者选择你的毒药:
$ pip install simqle
$ poetry add simqle
$ pipenv install simqle
simqle以yaml格式读取连接文件。见
.connections.yaml
文件部分了解更多详细信息。
用法
生产中
从连接的名称、sql语句和dict中获取结果 参数:
fromsimqleimportConnectionManager# Intialise your connectionscm=ConnectionManager(".connections.yaml")# Write some simple SQLsql="SELECT name, age FROM people WHERE category = :category"params={"category":5}result=cm.recordset(con_name="my-database",sql=sql,params=params)
recordset()
方法返回(数据、标题)的元组。数据”是行元组的列表。
标题是查询中字段名的列表。
发展中
将simqle_mode环境变量设置为“development”。这将使用 开发连接代替生产连接,而不改变 你的密码。
集成测试中
将simqle_mode环境变量设置为“testing”。
测试此包
测试需要行为包:
> pip install behave
要运行,只需:
> behave
.connections.yaml文件
定义生产、开发和测试服务器的连接字符串。这个
test-connections
和dev-connections
的名称应反映
connections
名字。每个连接都由其名称引用。
示例文件:
connections:# The name of the connection - this is what will be used in your project# to reference this connection.-name:my-sql-server-databasedriver:mssql+pyodbc:///?odbc_connect=connection:DRIVER={SQL Server};UID=<username>;PWD=<password>;SERVER=<my-server># some odbc connections require urls to be escaped, this is managed by# setting url_escaped = true:url_escape:true# File based databases like sqlite are slightly different - the driver# is very simple.-name:my-sqlite-databasedriver:sqlite:///# put a leading '/' before the connection for an absolute path, or omit# if it's relative to the project pathconnection:databases/my-database.db# This connection will be used if no name is given if the default # parameter is used:default:truedev-connections:# the names of the dev-connections should mirror the connections above.-name:my-sql-server-databasedriver:mssql+pyodbc:///?odbc_connect=# connecting to a different server hereconnection:DRIVER={SQL Server};UID=<username>;PWD=<password>;SERVER=<my-dev-server>url_escape:true-name:my-sqlite-databasedriver:sqlite:///connection:/tmp/my-dev-database.dbdefault:truetest-connections:-name:my-sql-server-databasedriver:mssql+pyodbc:///?odbc_connect=connection:DRIVER={SQL Server};UID=<username>;PWD=<password>;SERVER=<my-test-server>url_escape:true-name:my-sqlite-databasedriver:sqlite:///connection:/tmp/my-test-database.dbdefault:true
作者
贡献者
发布历史记录
- 0.1.0条
- 添加项目的基本框架
- 0.1.1条
- 单元测试
- 增加了sqlite的集成测试。
- 100%覆盖
- 0.2.0条
- 在connections.yaml文件中添加了url_escape选项
- 为mysql和postgresql添加集成测试
- 0.3.0分
- 项目重构为类
- 添加了默认参数
- 0.4.0条
- 作为连接模式添加的开发