Package: anodb
AnoDBConvenient Wrapper aroundaiosqland aDatabase Connection.DescriptionThis class creates a persistent database connection and imports
SQL queries from a file as simple Python functions.If the connection is broken, a new connection is attempted with increasing
throttling delays.Compared toaiosql, the point is not to need to pass a connection
as an argument on each call: TheDBclass embeds both connectionandquery methods.For concurrent programming (threads, greenlets…), a relevant setup
should also consider thread-locals and pooling issues at some higher level.ExampleInstall the module withpip install anodbor whatever method you like.
Once available:importanodb# parameters: driver, connection string, SQL filedb=anodb.DB("sqlite3","test.db","test.sql")db.do_some_insert(key=1,val="hello")db.do_some_update(key=1,val="world")print("data",db.do_some_select(key=1))db.commit()db.close()With filetest.sqlcontaining something like:-- name: do_some_selectSELECT*FROMStuffWHEREkey=:key;-- name: do_some_insert!INSERTINTOStuff(key,val)VALUES(:key,:val);-- name: do_some_update!UPDATEStuffSETval=:valWHEREkey=:key;DocumentationTheanodbmodule provides theDBclass which embeds both aPEP 249database connection
(providing methodscommit,rollback,cursor,closeand
itsconnectcounterpart to re-connect)andSQL queries wrapped
into dynamically generated functions byaiosql.
Such functions may be loaded from a string (add_queries_from_str) or a
path (add_queries_from_path).TheDBconstructor parameters are:dbthe name of the database driver:sqlite3,psycopg,pymysql, seeaiosql documentationfor a list of supported drivers.connan optional connection string used to initiate a connection with the
driver.
For instance,psycopgaccepts alibpq connection stringsuch as:"host=db1.my.org port=5432 dbname=acme user=calvin …".queriesa path name or list of path names from which to read query
definitions.optionsa dictionary or string to pass additional connection parameters.auto_reconnectwhether to attempt a reconnection if the connection is lost.
Default isTrue. Reconnection attempts are throttled exponentially
following powers of two delays from0.001and capped at30.0seconds.kwargs_onlywhether to only accept named parameters to python functions.exceptionfunction to re-process database exceptions.debugwhether to generate debugging messages.
Default isFalse.other named parameters are passed as additional connection parameters.importanodbdb=anodb.DB("sqlite3","acme.db","acme-queries.sql")db=anodb.DB("duckdb","acme.db","acme-queries.sql")db=anodb.DB("psycopg","host=localhost dbname=acme","acme-queries.sql")db=anodb.DB("psycopg",None,"acme-queries.sql",host="localhost",user="calvin",password="...",dbname="acme")db=anodb.DB("psycopg2","host=localhost dbname=acme","acme-queries.sql")db=anodb.DB("pygresql",None,"acme-queries.sql",host="localhost:5432",user="calvin",password="...",database="acme")db=anodb.DB("pg8000",None,"acme-queries.sql",host="localhost",port=5432,user="calvin",password="...",database="acme")db=anodb.DB("MySQLdb",None,"acme-queries.sql",host="localhost",port=3306,user="calvin",password="...",database="acme")db=anodb.DB("pymysql",None,"acme-queries.sql",host="localhost",port=3306,user="calvin",password="...",database="acme")db=anodb.DB("mysql-connector",None,"acme-queries.sql",host="localhost",port=3306,user="calvin",password="...",database="acme")db=anodb.DB("mariadb",None,"acme-queries.sql",host="localhost",port=3306,user="calvin",password="...",database="acme")VersionsSources,documentationandissuesare available onGitHub.Seeall versionsand
getpackagesfromPyPI.