跳转到主要内容

一个用于在.sql文件中执行自定义SQL查询的Django模块

项目描述

# django-query-execfile
**Django模块,用于在文件中执行SQL查询**

项目的完整文档可在[]找到

---

**注意**


---

# 概述

django-query-execfile是一个用于执行.sql查询文件的简单库。
特别是


# 要求

* Python (2.7.x )
* Django (1.5+ )

# 安装

使用`pip`安装...

```shell
pip install django-query-execfile
```


# 使用 & 示例

这是一个.sql文件的内容

```sql
#sum_customerPayment_amount_rate_exchanged_group_by_day
SELECT
DATE(customerPayment.created) AS created,
SUM(transaction.amount * transaction.exchange_rate) AS amount_exchanged
FROM shop_module_customerpayment customerPayment
LEFT JOIN shop_module_transaction transaction ON transaction.id = customerPayment.transaction_id
WHERE (customerPayment.created BETWEEN %(from_date)s AND %(to_date)s)
GROUP BY DATE(customerPayment.created);
```


使用示例

```python
from models import Order
from query_execfile import sql_execfile, raw_queryfile

result = sql_execfile('../datagrip/payment_transaction_stats.sql'),
params={'from_date': .., 'to_date': ..},
mapResultToDict=True,
includeDescription=True
)
print result

rawQuerySet = raw_queryfile(Order, '../datagrip/order_rawquery.sql')
遍历 rawQuerySet 中的 order
对 order 进行一些操作
```

**mapResultToDict=True** 将结果映射到一个字典,其中键为查询上方第一个注释。
因此,您可以在单个文件中写入多个查询,并一次性执行这些查询。

**includeDescription=True** 将在顶部添加一个额外的行作为列标题,使用查询的描述。
在这种情况下是 _AS created_ 和 _AS amount_exchanged_


**示例的返回结果:**

```json
{"sum_customerPayment_amount_rate_exchanged_group_by_day": [
[
"created",
"amount_exchanged"
],
[
"2016-03-01",
4933000.0
],
[
"2016-03-02",
7144000.0
],
...
...
...
[
"2016-03-10",
2110000.0
]
],}
```


# 文档 & 支持

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

django-query-execfile-0.6.tar.gz (4.5 kB 查看哈希)

上传时间 源代码

由...支持