Skip to main content

it enables sql files to be imported as a python module.

Project description

It enables SQL files to be imported as python modules.


  • Python 2.7

  • Python 3.6


$ pip install sql-importer


  • You have to make at the same directory which sql files have been placed on.

  • And write like the following to the

    ## if using django:
    # from django.db import connection
    import os
    from sql_importer import init
    init(os.path.dirname(__file__), globals(), connection, sql_type='postgresql')
  • That’s all, you can import sql files (removed .sql suffix) as python modules.

    • Example: testing/sql/sum_sales.sql exists.

    SELECT SUM(price) AS sum_sales FROM sales
      sales_from >= :'sales_from'
      AND sales_to < :'sales_to'
    from datetime import date
    from testing import sql
    sql.sum_sales.query(sales_from=date(2017, 5, 22), sales_to=date(2017, 12, 26))
    • sql object has 2 methods, both method execute the sql and receive variables as keyword arguments.


      It returns records. it expects only what has one or more results like select query.


      It returns number of records affected by the SQL.

  • Now sql_type argument allows postgresql.


start up

$ git clone
$ 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__.pyc  __pycache__        clear.sql  create_table.sql  delete.sql  drop_table.sql  insert.sql  select.sql  update.sql

 (venv) # python
>>> from tests.postgresql import sql
>>> sql.
sql.clear         sql.create_table  sql.drop_table    sql.init(         sql.os  
sql.connection    sql.delete          sql.insert        sql.psycopg2      sql.update

>>> sql.create_table.execute()
>>> sql.insert.execute(name='apple', price=100)
>>> list(
[{'name': 'apple', 'price': 100}]
>>> sql.delete.execute(name='orange')
>>> sql.delete.execute(name='apple')
>>> list(


(venv) # tox
  • This library is tested by only latest postgresql.


Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sql-importer-1.0.0.tar.gz (4.8 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page