{ "info": { "author": "Honza Pokorny", "author_email": "me@honza.ca", "bugtrack_url": null, "classifiers": [], "description": "anosql\n======\n\n.. image:: https://badge.fury.io/py/anosql.svg\n :target: https://badge.fury.io/py/anosql\n\n.. image:: http://readthedocs.org/projects/anosql/badge/?version=latest\n :target: http://anosql.readthedocs.io/en/latest/?badge=latest\n :alt: Documentation Status\n\n.. image:: https://travis-ci.org/honza/anosql.svg?branch=master\n :target: https://travis-ci.org/honza/anosql\n\nA Python library for using SQL\n\nInspired by the excellent `Yesql`_ library by Kris Jenkins. In my mother\ntongue, *ano* means *yes*.\n\nIf you are on python3.6+ or need ``anosql`` to work with ``asyncio`` based database drivers.\nSee the related project `aiosql `_.\n\nInstallation\n------------\n\n::\n\n $ pip install anosql\n\nUsage\n-----\n\nBasics\n******\n\nGiven a ``queries.sql`` file:\n\n.. code-block:: sql\n\n -- name: get-all-greetings\n -- Get all the greetings in the database\n SELECT * FROM greetings;\n\n -- name: $select-users\n -- Get all the users from the database,\n -- and return it as a dict\n SELECT * FROM USERS;\n\nWe can issue SQL queries, like so:\n\n.. code-block:: python\n\n import anosql\n import psycopg2\n import sqlite3\n\n # PostgreSQL\n conn = psycopg2.connect('...')\n queries = anosql.from_path('queries.sql', 'psycopg2')\n\n # Or, Sqlite3...\n conn = sqlite3.connect('cool.db')\n queries = anosql.from_path('queries.sql', 'sqlite3)\n\n queries.get_all_greetings(conn)\n # => [(1, 'Hi')]\n\n queries.get_all_greetings.__doc__\n # => Get all the greetings in the database\n\n queries.get_all_greetings.sql\n # => SELECT * FROM greetings;\n\n queries.available_queries\n # => ['get_all_greetings']\n\n\nParameters\n**********\n\nOften, you want to change parts of the query dynamically, particularly values in\nthe ``WHERE`` clause. You can use parameters to do this:\n\n.. code-block:: sql\n\n -- name: get-greetings-for-language-and-length\n -- Get all the greetings in the database\n SELECT *\n FROM greetings\n WHERE lang = %s;\n\nAnd they become positional parameters:\n\n.. code-block:: python\n\n visitor_language = \"en\"\n queries.get_all_greetings(conn, visitor_language)\n\n\n\nNamed Parameters\n****************\n\nTo make queries with many parameters more understandable and maintainable, you\ncan give the parameters names:\n\n.. code-block:: sql\n\n -- name: get-greetings-for-language-and-length\n -- Get all the greetings in the database\n SELECT *\n FROM greetings\n WHERE lang = :lang\n AND len(greeting) <= :length_limit;\n\nIf you were writing a Postgresql query, you could also format the parameters as\n``%s(lang)`` and ``%s(length_limit)``.\n\nThen, call your queries like you would any Python function with named\nparameters:\n\n.. code-block:: python\n\n visitor_language = \"en\"\n\n greetings_for_texting = queries.get_all_greetings(\n conn, lang=visitor_language, length_limit=140)\n\nUpdate/Insert/Delete\n********************\n\nIn order to run ``UPDATE``, ``INSERT``, or ``DELETE`` statements, you need to\nadd ``!`` to the end of your query name. Anosql will then execute it properly.\nIt will also return the number of affected rows.\n\nInsert queries returning autogenerated values\n*********************************************\n\nIf you want the auto-generated primary key to be returned after you run an\ninsert query, you can add ``