File: README.rst

package info (click to toggle)
aiopg 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 244 kB
  • sloc: python: 1,965; makefile: 3
file content (87 lines) | stat: -rw-r--r-- 2,637 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
aiopg
=====
.. image:: https://travis-ci.com/aio-libs/aiopg.svg?branch=master
   :target: https://travis-ci.com/aio-libs/aiopg
.. image:: https://codecov.io/gh/aio-libs/aiopg/branch/master/graph/badge.svg
   :target: https://codecov.io/gh/aio-libs/aiopg
.. image:: https://badges.gitter.im/Join%20Chat.svg
    :target: https://gitter.im/aio-libs/Lobby
    :alt: Chat on Gitter

**aiopg** is a library for accessing a PostgreSQL_ database
from the asyncio_ (PEP-3156/tulip) framework. It wraps
asynchronous features of the Psycopg database driver.

Example
-------

.. code:: python

    import asyncio
    import aiopg

    dsn = 'dbname=aiopg user=aiopg password=passwd host=127.0.0.1'

    async def go():
        pool = await aiopg.create_pool(dsn)
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("SELECT 1")
                ret = []
                async for row in cur:
                    ret.append(row)
                assert ret == [(1,)]

    loop = asyncio.get_event_loop()
    loop.run_until_complete(go())


Example of SQLAlchemy optional integration
------------------------------------------

.. code:: python

   import asyncio
   from aiopg.sa import create_engine
   import sqlalchemy as sa

   metadata = sa.MetaData()

   tbl = sa.Table('tbl', metadata,
       sa.Column('id', sa.Integer, primary_key=True),
       sa.Column('val', sa.String(255)))

   async def create_table(engine):
       async with engine.acquire() as conn:
           await conn.execute('DROP TABLE IF EXISTS tbl')
           await conn.execute('''CREATE TABLE tbl (
                                     id serial PRIMARY KEY,
                                     val varchar(255))''')

   async def go():
       async with create_engine(user='aiopg',
                                database='aiopg',
                                host='127.0.0.1',
                                password='passwd') as engine:

           async with engine.acquire() as conn:
               await conn.execute(tbl.insert().values(val='abc'))

               async for row in conn.execute(tbl.select()):
                   print(row.id, row.val)

   loop = asyncio.get_event_loop()
   loop.run_until_complete(go())

For ``yield from`` based code, see the ``./examples`` folder, files with
``old_style`` part in their names.

.. _PostgreSQL: http://www.postgresql.org/
.. _asyncio: http://docs.python.org/3.4/library/asyncio.html

Please use::

   $ make test

for executing the project's unittests.  See CONTRIBUTING.rst for details
on how to set up your environment to run the tests.