它使sql文件能够作为python模块导入。

sql-importer的Python项目详细描述


它允许将sql文件作为python模块导入。

https://circleci.com/gh/beproud/sql-importer.svg?style=svg

要求

  • python 2.7版
  • python 3.6

安装

$ pip install sql-importer

用法

  • 必须在SQL文件所在的同一目录下创建\uu init\uuu.py

  • 像下面这样写到.py

    ## if using django:# from django.db import connectionimportosfromsql_importerimportinitinit(os.path.dirname(__file__),globals(),connection,sql_type='postgresql')
  • 仅此而已,您可以将sql文件(删除.sql后缀)导入为python模块。

    • 示例: Test/SQL/SuMySalest.SQL。
    SELECTSUM(price)ASsum_salesFROMsalesWHEREsales_from>=:'sales_from'ANDsales_to<:'sales_to';
    fromdatetimeimportdatefromtestingimportsqlsql.sum_sales.query(sales_from=date(2017,5,22),sales_to=date(2017,12,26))
    • sql对象有两个方法,两个方法都执行sql并接收变量作为关键字参数。
      query:It returns records. it expects only what has one or more results like select query.
      execute:It returns number of records affected by the SQL.
  • 现在sql_type参数允许postgresql

演示

启动

$ git clone git@github.com:beproud/sql-importer.git
$ cd sql-importer
$ docker-compose up

准备

$ docker exec -it sqlimporter_app_1 /bin/bash
# python -m venv venv # only first time
# source venv/bin/activate

尝试

(venv)# ls tests/postgresql/sql
__init__.py  __init__.pyc  __pycache__        clear.sql  create_table.sql  delete.sql  drop_table.sql  insert.sql  select.sql  update.sql

 (venv)# python
>>>fromtests.postgresqlimportsql>>>sql.sql.clearsql.create_tablesql.drop_tablesql.init(sql.ossql.selectsql.connectionsql.deletesql.hostsql.insertsql.psycopg2sql.update>>>sql.create_table.execute()-1>>>sql.insert.execute(name='apple',price=100)1>>>list(sql.select.query())[{'name':'apple','price':100}]>>>sql.delete.execute(name='orange')0>>>sql.delete.execute(name='apple')1>>>list(sql.select.query())[]

单元测试

(venv)# tox
  • 此库仅由最新的PostgreSQL测试。

贡献者

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
在Java中为上传的文件设计强制的文件夹结构   java我如何在下面的代码中解决这个>错误HTTP Status 404   java如何在方面字段Lucene上添加分页   java My 安卓应用程序在尝试10次后崩溃   java“找不到Spring NamespaceHandler”错误   连接到Dynamodb时发生java AWS lambda错误   过程的价值。JAVA中的exitValue()   eclipse vscode java正在尝试设置项目   JavaEclipse不再自动在javadocs中添加标签   java找不到LoggerFactory类   在Java中实现延迟   设置onClickListener时的java NullPointerException   用jsoup解析HTML:Android和Java的区别