Package: simqle
SimQLeThe simple way to SQLPerfect for no fuss SQL in your Python projects. Execute SQL and return simple
record sets with named parameters. Manage several connections, and switch
between production, development and testing modes.Documentation can be foundhereInstallationRepositoryhttps://github.com/Harlekuin/SimQLeOr choose your poison:$ pip install simqle$ poetry add simqle$ pipenv install simqleSimQLe reads from a connections file in yaml format. See the.connections.yamlfile section for more details.UsageIn ProductionGet a result from the name of your connection, the SQL statement, and a dict
of parameters:fromsimqleimportConnectionManager# Intialise your connectionscm=ConnectionManager(".connections.yaml")# Write some simple SQLsql="SELECT name, age FROM people WHERE category = :category"params={"category":5}result=cm.recordset(con_name="my-database",sql=sql,params=params)# result.headings == ["name", "age"]# result.data == [#    ("Jim", 30),#    ("Bones", 35)# ]# result.as_dict == [#    {"name": "Jim", "age": 30},#    {"name": "Bones", "age": 35}# ]# result.column["name"] == ["Jim", "Bones"]Therecordset()method returns a RecordSet object with a bunch of handy methods for getting at the data.
There is also acm.record()method for queries you know only return a single record, and
acm.record_scalar()method for queries where you're after a single datum.In DevelopmentSet the SIMQLE_MODE environment variable to "development". This will use your
development connections in place of the production ones, without changing
your code.In Integration TestsSet the SIMQLE_MODE environment variable to "testing".Testing this packageTests require the behave package:> pip install behaveTo run, simply:> behaveThe .connections.yaml FileDefine the connection strings for production, development and test servers. The
names of thetest-connectionsanddev-connectionsshould mirror theconnectionsnames. Each connection is be referred to by its name.Example file:connections:# The name of the connection - this is what will be used in your project# to reference this connection.-name:my-sql-server-databasedriver:mssql+pyodbc:///?odbc_connect=connection:DRIVER={SQL Server};UID=<username>;PWD=<password>;SERVER=<my-server># some odbc connections require urls to be escaped, this is managed by# setting url_escaped = true:url_escape:true# File based databases like sqlite are slightly different - the driver# is very simple.-name:my-sqlite-databasedriver:sqlite:///# put a leading '/' before the connection for an absolute path, or omit# if it's relative to the project pathconnection:databases/my-database.db#  This connection will be used if no name is given if the default# parameter is used:default:truedev-connections:# the names of the dev-connections should mirror the connections above.-name:my-sql-server-databasedriver:mssql+pyodbc:///?odbc_connect=# connecting to a different server hereconnection:DRIVER={SQL Server};UID=<username>;PWD=<password>;SERVER=<my-dev-server>url_escape:true-name:my-sqlite-databasedriver:sqlite:///connection:/tmp/my-dev-database.dbdefault:truetest-connections:-name:my-sql-server-databasedriver:mssql+pyodbc:///?odbc_connect=connection:DRIVER={SQL Server};UID=<username>;PWD=<password>;SERVER=<my-test-server>url_escape:true-name:my-sqlite-databasedriver:sqlite:///connection:/tmp/my-test-database.dbdefault:trueConvenience functionsbind_sql(sql, params)can be used to bind named parameters (from a dictionary) to a SQL query, even if the library that executes
the query doesn't support named parameters. For example:importpandasaspdfromsimqleimportConnectionManager,bind_sqlcm=ConnectionManager()sql="SELECT Age FROM Person WHERE Name = :name"params={"name":"Hikaru Sulu"}bound_sql=bind_sql(sql,params)# Note we don't need to pass the params here, they have already been bound:df=pd.read_sql(con=cm.get_engine(),sql=bound_sql)MetricsUseful metrics like the execution time and actual SQL sent are logged to the logging namespace "simqle".ChatSay hello in the Gary:https://gitter.im/SimQLe/communityAuthorTom MalkinContributorsZack BotkinRelease History0.1.0Add the basic skeleton of the project0.1.1Unit testsIntegration tests for sqlite added.100% coverage0.2.0Added url_escape option in connections.yaml fileIntegration tests added for mysql and postgresql0.3.0Project refactored into classesDefault parameter added0.4.0Development added as a connection mode0.5.0RecordSet, Record and RecordScalar objects added0.5.3bind_sql is exposed for convenience