Package: aio-databases
AIO-DatabasesThe package gives you async support for a range of databases (SQLite,
PostgreSQL, MySQL).FeaturesHas no dependencies (except databases drivers)SupportsasyncioandtrioSupportsaiosqlite,aiomysql,aiopg,asyncpg,triopg,trio_mysqlManage pools of connectionsManage transactionsRequirementspython >= 3.7Installationaio-databasesshould be installed using pip:$pipinstallaio-databasesYou have to choose and install the required database drivers with:# To support SQLite$pipinstallaio-databases[aiosqlite]# asyncio# To support MySQL$pipinstallaio-databases[aiomysql]# asyncio$pipinstallaio-databases[trio_mysql]# trio# To support PostgreSQL (choose one)$pipinstallaio-databases[aiopg]# asyncio$pipinstallaio-databases[asyncpg]# asyncio$pipinstallaio-databases[triopg]# trio# To support ODBC (alpha state)$pipinstallaio-databases[aioodbc]# asyncioUsageInit a databasefromaio_databasesimportDatabase# Initialize a databasedb=Database('sqlite:///:memory:')# with default driver# Flesh out the driverdb=Database('aiosqlite:///:memory:',**driver_params)Setup a pool of connections (optional)Setup a pool of connections# Initialize a database's poolasyncdefmy_app_starts():awaitdb.connect()# Close the poolasyncdefmy_app_ends():awaitdb.disconnect()# As an alternative users are able to use the database# as an async context managerasyncwithdb:awaitmy_main_coroutine()Get a connection# Acquire and release (on exit) a connectionasyncwithdb.connection():awaitmy_code()# Acquire a connection only if it not existasyncwithdb.connection(False):awaitmy_code()If a pool is setup it will be usedRun SQL queriesawaitdb.execute('select $1','1')awaitdb.executemany('select $1','1','2','3')records=awaitdb.fetchall('select (2 * $1) res',2)assertrecords==[(4,)]record=awaitdb.fetchone('select (2 * $1) res',2)assertrecord==(4,)assertrecord['res']==4result=awaitdb.fetchval('select 2 * $1',2)assertresult==4Iterate through rows one by oneasyncforrecindb.iterate('select name from users'):print(rec)Manage connectionsBy default the database opens and closes a connection for a query.# Connection will be acquired and released for the queryawaitdb.fetchone('select%s',42)# Connection will be acquired and released againawaitdb.fetchone('select%s',77)Manually open and close a connection# Acquire a new connection objectasyncwithdb.connection():# Only one connection will be usedawaitdb.fetchone('select%s',42)awaitdb.fetchone('select%s',77)# ...# Acquire a new connection or use an existingasyncwithdb.connection(False):# ...If there any connection alreadydb.methodwould be using the current oneasyncwithdb.connection():# connection would be acquired hereawaitdb.fetchone('select%s',42)# the connection is usedawaitdb.fetchone('select%s',77)# the connection is used# the connection released thereManage transactions# Start a tranction using the current connectionasyncwithdb.transaction()astrans1:# do some work ...asyncwithdb.transaction()astrans2:# do some work ...awaittrans2.rollback()# unnessesary, the transaction will be commited on exit from the# current contextawaittrans1.commit()# Create a new connection and start a transactionasyncwithdb.tranction(True)astrans:# do some work ...Bug trackerIf you have any suggestions, bug reports or annoyances please report them to
the issue tracker athttps://github.com/klen/aio-databases/issuesContributingDevelopment of the project happens at:https://github.com/klen/aio-databasesLicenseLicensed under aMIT License